I. Problem Description
During normal development, we can find that many developers did not seriously solve the exit of the program. Generally, it is either a simple finish (just exit the current activity) or another method, such:
1. Method 1: first obtain the id of the current Process, and then kill the Process: android. OS. Process. killProcess (android. OS. Process. myPid ());
2. Method 2: Terminate the currently running Java virtual machine, resulting in program termination: System. exit (0 );
3. Method 3: Force disable all executions associated with the package: ActivityManagermanager = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
Manager. restartPackage (getPackageName ());
To use this method, you must add the following permissions: <uses-permissionandroid: name = "android. permission. RESTART_PACKAGES"/>
However, these methods are flawed and cannot completely exit the program. For example, method 1 does not empty the task stack of the activity of the current application. For the third method, it can only kill other applications but not itself.
Ii. Solutions
1. Implementation idea: Since you cannot close all the activities at a time, the general solution for an enterprise is to record all opened activities and manage the activities in singleton mode, close all opened activities when exiting the program.
2. Code:
(1) create a new class App that inherits the Application. It is used to create a global instance of the entire Application and needs to be added to the AndroidManifest. xml list.
App Type:
[Html]
Package com. example. testexit;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. app. Application;
Public class App extends Application {
ArrayList <Activity> activities;
Private static App instance;
/*
* Run the command when the entire application is created.
*/
@ Override
Public void onCreate (){
Activities = new ArrayList <Activity> ();
GetInstance ();
Super. onCreate ();
}
Public static App getInstance (){
If (null = instance ){
Instance = new App ();
}
Return instance;
}
Public void exitApplication (){
List <Activity> lists = instance. activities;
For (Activity a: lists ){
A. finish ();
}
}
}
Package com. example. testexit;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. app. Application;
Public class App extends Application {
ArrayList <Activity> activities;
Private static App instance;
/*
* Run the command when the entire application is created.
*/
@ Override
Public void onCreate (){
Activities = new ArrayList <Activity> ();
GetInstance ();
Super. onCreate ();
}
Public static App getInstance (){
If (null = instance ){
Instance = new App ();
}
Return instance;
}
Public void exitApplication (){
List <Activity> lists = instance. activities;
For (Activity a: lists ){
A. finish ();
}
}
}
In other activities, add the current Activity to the onCreate method, and then remove the Activity from the onDestroy method.
MainActivity:
[Java]
Package com. example. testexit;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
App app = (App) getApplication ();
App. activities. add (this );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
@ Override
Protected void onDestroy (){
Super. onDestroy ();
App app = (App) getApplication ();
App. activities. remove (this );
}
}
Package com. example. testexit;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
App app = (App) getApplication ();
App. activities. add (this );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
@ Override
Protected void onDestroy (){
Super. onDestroy ();
App app = (App) getApplication ();
App. activities. remove (this );
}
}
Activity1:
[Java]
Package com. example. testexit;
Import android. app. Activity;
Import android. OS. Bundle;
Public class Activity1 extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
App app = (App) getApplication ();
App. activities. add (this );
}
@ Override
Protected void onDestroy (){
Super. onDestroy ();
App app = (App) getApplication ();
App. activities. remove (this );
}