Here we will introduce two methods: one is to remember each activity and then kill them one by one; the other is to use broadcast. This article comes from the network. If there are similarities, It is necessary. I wrote this post just to summarize the common things. I still hope the original author is not strange. I really do not want to infringe.
Method 1: Use list to save activity instances, and then kill them one by one.
Code:
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 to 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:
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.
Method 2: Use Broadcast
2.2 exiting the entire application will inevitably be confusing. The previous attempt was as follows:
ActivityManager manager = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE );
Manager. killBackgroundProcesses (package );
No
Android. OS. Process. killProcess (android. OS. Process. myPid ());
No.
Manager. restartPackage (package );
Still not good
Intent MyIntent = new Intent (Intent. ACTION_MAIN );
MyIntent. addCategory (Intent. CATEGORY_HOME );
StartActivity (MyIntent );
Finish ();
This is only returned to the desktop. If you open multiple activities and close them again, it will be a problem, or it will not work.
We can see that there is a broadcast mechanism, and we can find that it is a good thing to completely solve this problem, without talking nonsense about the Code:
Public abstract class EnterActivity extends BaseActivity {
...
// Write an internal broadcast class. When an action is received, the activity ends.
Private BroadcastReceiver broadcastReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
UnregisterReceiver (this); // this statement must be written to avoid errors. If it is not written, a bunch of errors will be reported even if it can be closed.
(Activity) context). finish ();
}
};
@ Override
Public void onResume (){
Super. onResume ();
// Register the broadcast in the current activity
IntentFilter filter = new IntentFilter ();
Filter. addAction (Attribute. PAGENAME );
RegisterReceiver (this. broadcastReceiver, filter); // register
}
/**
* Close
*/
Public void close (){
Intent intent = new Intent ();
Intent. setAction (Attribute. PAGENAME); // specifies the action
SendBroadcast (intent); // This function is used to send broadcasts.
Finish ();
}
...
}