"Go" Android completely quits the app

Source: Internet
Author: User
Tags exit in

Original URL: http://www.yoyong.com/archives/199

The Android Exit application calls Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0), which is only for the first activity ( Activity at the entrance). If you have a,b,c three activity, and you want activity to exit in B or C, calling the method above will often destroy the current activity to return to the previous activity. Of course, you can return to the last activity one by one until you jump to the activity in the portal and exit the application. But it's more troublesome and the experience of returning one by one is not friendly.

The popular method on the web is to define stacks, write a exitapplication class, manage the activity with a singleton pattern, and Invoke exitapplication.getinstance () in each oncreate () method of the activity. The addactivity (This) method, which calls the Exitapplication.getinstance (). Exit () method on exit, to exit the application completely.
Exitapplication class
The code is as follows:

View Plaincopy to Clipboardprint?
  1. Import java.util.LinkedList;
  2. Import java.util.List;
  3. Import android.app.Activity;
  4. Import android.app.Application;
  5. Public class Exitapplication extends application {
  6. private List activitylist = new LinkedList ();
  7. private static exitapplication instance;
  8. private exitapplication ()
  9. {
  10. }
  11. //Get a unique exitapplication instance in a singleton mode
  12. public static Exitapplication getinstance ()
  13. {
  14. if (null = = instance)
  15. {
  16. Instance = new Exitapplication ();
  17. }
  18. return instance;
  19. }
  20. //Add activity to the container
  21. public void addactivity (activity activity)
  22. {
  23. Activitylist.add (activity);
  24. }
  25. //Traverse all activity and finish
  26. public Void exit ()
  27. {
  28. For (Activity activity:activitylist)
  29. {
  30. Activity.finish ();
  31. }
  32. System.exit (0);
  33. }
  34. }

The following three classes of indexactivity, bactivity,cactivity are simple examples, respectively, are the indexactivity–>bactivity–>cactivity of the jump order. The Exitapplication.getinstance (). Addactivity (activity activity) method is called in the OnCreate () method in each activity class. When exiting an application in any of the activity interfaces, simply call the Exitapplication.getinstance (). Exit () method to completely exit the application in any activity.
Indexactivity Class Source code:

View Plaincopy to Clipboardprint?
  1. Import android.app.Activity;
  2. Import android.content.Intent;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Button;
  7. Public class Indexactivity extends Activity {
  8. /** Called when the activity is first created. * /
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. Button next= (button) Findviewbyid (R.id.next_to_b);
  14. Next.setonclicklistener (Nextclick);
  15. Button exit= (button) Findviewbyid (R.id.exit_main);
  16. Exit.setonclicklistener (Exitclick);
  17. Exitapplication.getinstance (). Addactivity (this);
  18. }
  19. Onclicklistener nextclick=New Onclicklistener () {
  20. @Override
  21. public void OnClick (View v) {
  22. //TODO auto-generated method stub
  23. Intent intent=New Intent (indexactivity. This,bactivity.  class);
  24. StartActivity (Intent);
  25. }
  26. };
  27. Onclicklistener exitclick=New Onclicklistener () {
  28. @Override
  29. public void OnClick (View v) {
  30. //TODO auto-generated method stub
  31. Exitapplication.getinstance (). exit ();
  32. }
  33. };
  34. }

Bactivity Class Source code:

View Plaincopy to Clipboardprint?
  1. Import android.app.Activity;
  2. Import android.content.Intent;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Button;
  7. Public class Bactivity extends Activity {
  8. @Override
  9. protected void OnCreate (Bundle savedinstancestate) {
  10. TODO auto-generated Method Stub
  11. Super.oncreate (savedinstancestate);
  12. Setcontentview (r.layout.b);
  13. Button next_to_c= (button) Findviewbyid (R.id.next_to_c);
  14. Next_to_c.setonclicklistener (Next_to_cclick);
  15. Button exit_b= (button) Findviewbyid (r.id.exit_b);
  16. Exit_b.setonclicklistener (Exitclick);
  17. Exitapplication.getinstance (). Addactivity (this);
  18. }
  19. Onclicklistener next_to_cclick=New Onclicklistener () {
  20. @Override
  21. Public void OnClick (View v) {
  22. TODO auto-generated Method Stub
  23. Intent intent=New Intent (bactivity. This,cactivity.  class);
  24. StartActivity (Intent);
  25. }
  26. };
  27. Onclicklistener exitclick=New Onclicklistener () {
  28. @Override
  29. Public void OnClick (View v) {
  30. TODO auto-generated Method Stub
  31. Exitapplication.getinstance (). exit ();
  32. }
  33. };
  34. }

Cactivity Class Source code:

View Plaincopy to Clipboardprint?
  1. Import android.app.Activity;
  2. Import Android.os.Bundle;
  3. Import Android.view.View;
  4. Import Android.view.View.OnClickListener;
  5. Import Android.widget.Button;
  6. Public class Cactivity extends activity{
  7. @Override
  8. protected void OnCreate (Bundle savedinstancestate) {
  9. TODO auto-generated Method Stub
  10. Super.oncreate (savedinstancestate);
  11. Setcontentview (R.LAYOUT.C);
  12. Button exit_c= (button) Findviewbyid (R.id.exit_c);
  13. Exit_c.setonclicklistener (Exitclick);
  14. Exitapplication.getinstance (). Addactivity (this);
  15. }
  16. Onclicklistener exitclick=New Onclicklistener () {
  17. @Override
  18. Public void OnClick (View v) {
  19. TODO auto-generated Method Stub
  20. Exitapplication.getinstance (). exit ();
  21. If you just call one of the following methods, the app does not exit completely
  22. Android.os.Process.killProcess (Android.os.Process.myPid ());
  23. System.exit (0);
  24. }
  25. };
  26. }

Source Download : Exitactivity.zip

"Go" Android completely quits the app

Related Article

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.