This article describes the method of quitting the entire application by programming Android. Share it for your reference, as follows:
When writing Android applications, we often encounter the desire to exit the current ACitivity or directly exit the application. My previous general operations were to press the return key, or directly press the home key to return directly. In fact, neither of these operations closed the current application and did not release the system resources. Sometimes when there are many activities to jump, you need to press the return key multiple times, which makes it feel unpleasant at all.
A menu return function key was added later. This method can only use (0) to close the currently active activity. The code is as follows:
public boolean onCreateOptionsMenu(Menu menu) { (menu); MenuItem item = (, , , "Exit"); (new () { public boolean onMenuItemClick(MenuItem item) { (0); return true; } }); return true; }
Thinking that this is too inconvenient, the application is still running after exiting the interface. Today, I checked the information and finally solved the problem.
There are several methods provided online. I have absorbed the following method that I think is simple and easy to understand, copied the following SysApplication class into the project, and then added the current Activity to the animationlist in the oncreate method of each Activity through().addActivity(this); and finally called().exit(); when I want to exit, I can directly close all Activity and exit the application.
Attached code:
import ; import ; import ; import ; import ; import ; import ; public class SysApplication extends Application { private List<Activity> mList = new LinkedList<Activity>(); private static SysApplication instance; private SysApplication() { } public synchronized static SysApplication getInstance() { if (null == instance) { instance = new SysApplication(); } return instance; } // add Activity public void addActivity(Activity activity) { (activity); } public void exit() { try { for (Activity activity : mList) { if (activity != null) (); } } catch (Exception e) { (); } finally { (0); } } public void onLowMemory() { (); (); } }
Add in the oncreate activity in the application
like:
public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); ().addActivity(this); }
I hope this article will be helpful to everyone's Android programming design.