Android app exit
For beginners, the exit of android apps is sometimes a headache. Below are some common exit methods:
In the big direction, the exit idea is nothing more than destroying all the activities of the program. The idea is clear and you can see what tricks you have taken ~
1. Manufacturing throws exceptions and causes the entire program to exit
2. Put all the activities into a list, and then finish all the activities when they need to exit.
3. Broadcasting is used to exit the function.
1. The entire process exits due to manufacturing exception:
See http://blog.csdn.net/liuhe688/article/details/6584143
2. Put all the activities into a list, and then finish all the activities when they need to exit:
Define a set in application
When activity oncreate is used, the corresponding activity is added to the set. When the activity exits, it traverses the previously opened activity and closes it.
For (Activity activity: leCacheApp.OpenActivity){
Activity. finish ();}
Android. OS. Process. killProcess (android. OS. Process. myPid ());
System. exit (0 );
3. Broadcast to exit the function:
When each Activity is created (onCreate), it registers a broadcast receiver for the Activity and sends the broadcast when it exits. The approximate code is as follows:
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
IntentFilter filter = new IntentFilter ();
Filter. addAction ("finish ");
Registerpolicer (mfinishpolicer, filter );
}
Private BroadcastReceiver mFinishReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
If ("finish". equals (intent. getAction ())){
Log. e ("########", "I am" + getLocalClassName () + ", now finishing myself ...");
Finish ();
}
}
};
When you exit, run the following code to close the full exit program on all interfaces:
GetApplicationContext (). sendBroadcast (new Intent ("finish "));