Android exits the App perfectly (Exit)

Source: Internet
Author: User

In the last two days, the problem of exiting the app on Android has been exhausted. I have been searching for the app on google & baidu for a long time, and I have come up with three methods, however, after my experiments, I cannot.

The three methods are:

KillProcess. In this way, after you kill, the Activity will return to the previous Activity.
Android Level 8 (including 8) is operated by an API. Level8 and later are another one, so it cannot be used
Use FLAG_ACTIVITY_CLEAR_TOP, from A to B
The following describes your methods:
We all know that the Android Activity has A historical stack. For example, after finishing A-> B-> C and C, return to B and finish all the activities, the program naturally exits. Of course, you also need to determine whether to use other resources of your program. Therefore, you need to find a way to save the Activity. Then, call the finish () method where the program exits.
Use global variables. By the way, the first thing that comes to mind is to inherit the Application and input the code.
[Java]
Public class AgentApplication extends Application {
 
Private List <Activity> activities = new ArrayList <Activity> ();
 
Public void addActivity (Activity activity ){
Activities. add (activity );
}
 
@ Override
Public void onTerminate (){
Super. onTerminate ();

For (Activity activity: activities ){
Activity. finish ();
}

OnDestroy ();

System. exit (0 );
}
}

Then, addActivity () is called during Activity onCreate. Some people may think that this Application should be used in all activities onCreate, and a singleton instance needs to be created. In fact, it is not required at all. Use this. getApplication () in the Activity.
Finally, you can call application. onTerminate () Where you need to launch the program. Remember: super. onTerminate () must be called. onDestroy () in the Code is my own method to release other resources, not the system.

After running the preceding code, a message is displayed in LogCat:
The Process package name (pid xxxxx) has died. proves that your program has exited. Now you can test it.

I hope it will be useful to you. At the same time, I would like to thank my colleague Hu Nan for helping me solve other problems, so that I have time to write a blog. Thank you.


From column zeus_9i

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.