When developing the andorid program, it will inevitably involve exiting the entire application. How can we completely exit? Refer to the following key code:
[Java] import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Application;
Import android. content. DialogInterface;
Import android. content. Intent;
Public class SysApplication extends Application {
Private List <Activity> mList = new Entity List <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 ){
MList. add (activity );
}
Public void exit (){
Try {
For (Activity activity: mList ){
If (activity! = Null)
Activity. finish ();
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
System. exit (0 );
}
}
@ Override
Public void onLowMemory (){
Super. onLowMemory ();
System. gc ();
}
}
Import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Application;
Import android. content. DialogInterface;
Import android. content. Intent;
Public class SysApplication extends Application {
Private List <Activity> mList = new Entity List <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 ){
MList. add (activity );
}
Public void exit (){
Try {
For (Activity activity: mList ){
If (activity! = Null)
Activity. finish ();
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
System. exit (0 );
}
}
@ Override
Public void onLowMemory (){
Super. onLowMemory ();
System. gc ();
}
} Add similar code in the onCreate method of each Activity:
[Java] public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SysApplication. getInstance (). addActivity (this );
}
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SysApplication. getInstance (). addActivity (this );
}
To exit the program, call:
[Java] SysApplication. getInstance (). exit ();
SysApplication. getInstance (). exit ();
In short, you can add reference of each Activity to a global linked list in singleton mode. Each time you exit a program and call System. exit (0), you must first call the finish method of the Activity in the linked list.
From the pure soul