Several ways Android safely exits the application
To close the application gracefully:
When your app is no longer in use, you usually need to close the app, and you can turn off the Android app in three ways:
The first method: Get the ID of the current process first, and then kill the process.
Android.os.Process.killProcess (Android.os.Process.myPid ())
The application cannot be completely killed in this way, and he will not empty the task stack of the activity that the application is currently applying to
Second method: Terminates the currently running Java Virtual machine, causing the program to terminate
System.exit (0);
Don't kill your own app
The third approach: forcing all executions that are associated with the package to be closed
Activitymanager manager = (Activitymanager) getsystemservice (Context.activity_service);
Manager.restartpackage (Getpackagename ());
<uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>
You can only kill others and not kill yourself.
practices in the enterprise:
Create Class App inheritance application
Androidmainfest-->name-->app//declares that the class adds android:name= ". Global instance App" for the entire application global instance
Create an instance of the entire application global
App.class:
Arraylist<activity> activities;
In the other activity:
public void OnCreate (Bundle savedinstancestate) {
App app = (APP) getapplication ();//Get Application Global Instance reference
App.activities.add (this); Put the current activity into the collection
}
public void Ondestory () {
App app = (APP) getapplication ();//Get Application Global Instance reference
App.activities.remove (this); Remove the current activity from the collection
}
Safe exit:
The following code is defined in the event of the menu Exit button:
App app = (APP) getapplication ();
list<activity> activities = app.activities;
for (Activity act:activities) {
Act.finish ();//Explicit end
}
Several ways Android safely exits the application