From http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 158206.
Original Author Android-sylar
I have seen a lot of questions about application exit. I would like to give you a brief summary here today. I hope you will forgive me for saying something wrong.
Method 1: system. exit (0) and Android. OS. process. killprocess (Android. OS. process. mypid (), I think many people have tried. When multiple activities are closed, these two methods do not work at all, because of course they are related to the stack management of the activity.
Method 2: restartpackage. This method terminates all processes associated with this package, all processes that share the same uid are killed, and all activities are removed and all services created are stopped, A broadcast intent will also be sent. action_package_restarted. As we all know, this method is outdated and 2.2 does not work.
- Activitymanager manager = (activitymanager) getsystemservice (activity_service );
- Manager. restartpackage (getpackagename ());
- <Uses-Permission Android: Name = "android. Permission. restart_packages"/>
Copy code
Method 3: replace restartpackage after 2.2. But Google only gives an empty shell, and this method still does not work.
- Activitymanager. killbackgroundprocesses (packagename );
- <Uses-Permission Android: Name = "android. Permission. kill_background_processes"/>
Copy code
Method 4: Create an activityinstance Singleton mode to uniformly manage the exit of the activity. This is a very bad method, because it is indeed useful, but it is a waste of writing it.
The specific method is to write an activityinstance singleton and call its activityinstance when each activity starts. addactivity (this); method. When exiting the application, activityinstance is called. exit (); method.
- Public void exit (){
- For (activity at: activitylist ){
- At. Finish ();
- }
- System. Exit (0 );
- }
Copy code
Method 5: Send the broadcast and exit. Send a specific broadcast when the application needs to end. Each activity is closed after receiving the broadcast. If you don't have the trouble, you can do this. In my opinion, there is no better way.
Method 6: Exit recursively. Use startactivityforresult when opening a new activity, add a flag, process it in onactivityresult, and disable it recursively.
Method 7: Jump back to home when exiting, a false exit method, but can exit. This method only returns to the home page and falsely exits its own applications.
- Intent startmain = new intent (intent. action_main );
- Startmain. addcategory (intent. category_home );
- Startmain. setflags (intent. flag_activity_new_task );
- Startactivity (startmain );
- System. Exit (0 );
Copy code
Method 8: activitymanager. forcestoppackage (packagename); check whether forcestop is quite familiar. In our system settings, closing the application is called. This method can completely close the application with only one package name.
However, this method is hidden by the system. We cannot find this method in activitymanager when writing an application. This involves calling the system to hide the API. The following method is obtained through the reflection mechanism.
- Activitymanager SD = (activitymanager) This. getsystemservice (activity_service );
- Method method = Class. forname ("android. App. activitymanager ")
- . Getmethod ("forcestoppackage", String. Class );
Copy code
Supplement: in fact, Android has its own memory management mechanism and does not need to completely exit its own application. When a user presses the return key, the user can return to home or other related interfaces. You can quit falsely without special requirements.