Android Program exit perfect solution compatible with all Sdk_android

Source: Internet
Author: User
Learn a lot on the internet and how the Android activity exits. Many methods are not good, try a variety of methods, the following method is my favorite, simple and easy to understand.


uses a single case pattern to create an activity management object, which has one activity container (implementing itself, using LinkedList, and so on) dedicated to storing each of the newly opened operations, and easy to understand, easy to operate, very good!


MyApplication Class (stores each activity, and implements the operation to close all of the actions


Copy Code code as follows:

public class MyApplication extends application {
Private list<activity> activitylist = new linkedlist<activity> ();
private static MyApplication instance;
Private MyApplication ()
{
}
To get a unique MyApplication instance in a single case pattern
public static MyApplication getinstance ()
{
if (null = = instance)
{
Instance = new MyApplication ();
}
return instance;
}
Add an activity to the container
public void addactivity (activity activity)
{
Activitylist.add (activity);
}
Traverse all activity and finish
public void exit ()
{
For (activity activity:activitylist)
{
Activity.finish ();
}
System.exit (0);
}
}



adds the activity to the MyApplication object instance container in the OnCreate method in each activity


Copy Code code as follows:

Myapplication.getinstance (). addactivity (this);

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.