How to end all activity in Android development

Source: Internet
Author: User

Each activity has its own life cycle, which is opened and eventually closed.

Four ways to end the current activity

Java code
    1. Close the current Activity method one
    2. Finish ();
    3. Close the current interface method two
    4. Android.os.Process.killProcess (Android.os.Process.myPid ());
    5. Close the current interface method three
    6. System.exit (0);
    7. Close the current interface method four
    8. This.ondestroy ();

But if four activity:a,b,c and d have been activated,

In D activity, you want to start another activity B, but not become a a,b,c,d,b, but hope is a, B, and the data is still retained

Java code
    1. Intent Intent = new Intent ();
    2. Intent.setclass (tableactivity. This, frameactivity.  class);
    3. Intent.addflags (Intent.flag_activity_single_top); //Set do not refresh the interface that will be skipped
    4. Intent.setflags (Intent.flag_activity_clear_top); //It can turn off activity in the middle of the desired interface
    5. StartActivity (Intent);

If four activity:a,b,c and d have been started,

In D activity, want to start another activity B, but not become a,b,c,d,b, but hope is a A, and B data does not retain

Java code
    1. Intent Intent = new Intent ();
    2. Intent.setclass (tableactivity. This, frameactivity.  class);
    3. Intent.setflags (Intent.flag_activity_clear_top); //It can turn off activity in the middle of the desired interface
    4. StartActivity (Intent);


If four activity:a,b,c and d have been started, in D activity,
/Want to start another Activity B, but not become a,b,c,d,b, but hope is a,c,d,b, you can write code like this:

Java code
    1. Intent intent1 = new Intent (tableactivity. This, frameactivity.    class);
    2. Intent1.addflags (Intent.flag_activity_reorder_to_front);
    3. StartActivity (INTENT1);

If you have started four activity:a,b,c and D, in D activity, you want to shut down all activity at once .

Create a class that is designed to handle activity

Java code
  1. Package com.layout;
  2. Import java.util.LinkedList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import android.app.Application;
  6. /**
  7. * A class used to end all background activity
  8. * @author Administrator
  9. *
  10. */
  11. Public class Sysapplication extends application {
  12. //Use list to save each activity is the key
  13. private List<activity> mlist = new linkedlist<activity> ();
  14. //In order to implement static objects created every time you use the class without creating a new object
  15. private static sysapplication instance;
  16. //Construction method
  17. Private Sysapplication () {}
  18. //Instantiate once
  19. public synchronized static Sysapplication getinstance () {
  20. if (null = = instance) {
  21. Instance = new Sysapplication ();
  22. }
  23. return instance;
  24. }
  25. //Add Activity
  26. public void addactivity (activity activity) {
  27. Mlist.add (activity);
  28. }
  29. //close activity in each list
  30. public Void exit () {
  31. try {
  32. For (Activity activity:mlist) {
  33. if (activity! = null)
  34. Activity.finish ();
  35. }
  36. } catch (Exception e) {
  37. E.printstacktrace ();
  38. } finally {
  39. System.exit (0);
  40. }
  41. }
  42. //Kill process
  43. public void Onlowmemory () {
  44. super.onlowmemory ();
  45. System.GC ();
  46. }
  47. }

When each activity is created, add the

Java code
    1. Sysapplication.getinstance (). Addactivity (this);

Add the activity to the list.

When you want to close, call the Sysapplication exit method

Java code
    1. Close the entire program
    2. Sysapplication.getinstance (). exit ();

How to end all activity in Android development

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.