Solutions for Android Enterprise-class programs to exit completely

Source: Internet
Author: User

First, the problem description

In the normal development process can be found that many developers of the program is not to seriously resolve the exit. It's usually either a simple finish (just out of the current activity), or something else, like:

1, the first method: first get the ID of the current process, and then kill the process: Android.os.Process.killProcess (Android.os.Process.myPid ());

2, the second method: terminate the currently running Java Virtual machine, causing the program to terminate: System.exit (0);

3. The third method: Force close all execution associated with the package: Activitymanagermanager = (Activitymanager) getsystemservice (Context.activity_service);

Manager.restartpackage (Getpackagename ());

Use this method to add permissions: <uses-permissionandroid:name= "Android.permission.RESTART_PACKAGES"/>

However, these methods are flawed and do not completely exit the program, such as method one, which does not empty the task stack of the current application's activity. For the third method, it can only kill other applications and not kill itself.

Second, the solution

1, realize the idea: since not all the activity can be closed all at once, the general enterprise's solution is to record all open activity, using a singleton mode to manage the activity, and then exit the program when all open activity is closed off.

2. Code:

⑴ creates a new class app that inherits application, which is created as a global instance of the entire application and needs to be added to the Androidmanifest.xml list.

App class:

[HTML]View Plaincopy
  1. Package com.example.testexit;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import android.app.Application;
  6. public class App extends application {
  7. ArrayList<Activity> activities;
  8. private static APP instance;
  9. /*
  10. * Executed when the entire application is created
  11. */
  12. @Override
  13. public void OnCreate () {
  14. activities = new ArrayList<Activity> ();
  15. GetInstance ();
  16. Super.oncreate ();
  17. }
  18. public static App getinstance () {
  19. if (null = = instance) {
  20. instance = new App ();
  21. }
  22. return instance;
  23. }
  24. public void Exitapplication () {
  25. List<Activity> lists = instance.activities;
  26. for (Activity a:lists) {
  27. A.finish ();
  28. }
  29. }
  30. }

In other activity, add the current activity in the OnCreate method and remove the activity from the OnDestroy method.

Mainactivity:

[Java]View Plaincopy
  1. Package com.example.testexit;
  2. Import Android.os.Bundle;
  3. Import android.app.Activity;
  4. Import Android.view.Menu;
  5. Public class Mainactivity extends Activity {
  6. @Override
  7. protected void OnCreate (Bundle savedinstancestate) {
  8. super.oncreate (savedinstancestate);
  9. Setcontentview (R.layout.activity_main);
  10. App app = (APP) getapplication ();
  11. App.activities.add (this);
  12. }
  13. @Override
  14. Public Boolean oncreateoptionsmenu (Menu menu) {
  15. Getmenuinflater (). Inflate (R.menu.main, menu);
  16. return true;
  17. }
  18. @Override
  19. protected void OnDestroy () {
  20. Super.ondestroy ();
  21. App app = (APP) getapplication ();
  22. App.activities.remove (this);
  23. }
  24. }

Activity1:

[Java]View Plaincopy
  1. Package com.example.testexit;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Public class Activity1 extends Activity {
  5. @Override
  6. protected void OnCreate (Bundle savedinstancestate) {
  7. super.oncreate (savedinstancestate);
  8. App app = (APP) getapplication ();
  9. App.activities.add (this);
  10. }
  11. @Override
  12. protected void OnDestroy () {
  13. Super.ondestroy ();
  14. App app = (APP) getapplication ();
  15. App.activities.remove (this);
  16. }
  17. }

You can also refer to a few other articles about this: http://www.2cto.com/kf/201108/99444.html

http://blog.csdn.net/sgl870927/article/details/6281971

Http://www.cnblogs.com/qingblog/archive/2012/06/08/2541790.html

Solutions for Android Enterprise-class programs to exit completely

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.