Implement the activity Manager to exit all activity_android at once

Source: Internet
Author: User

about how to quit all activity on the internet there are many different kinds of statements, such as the way to kill the process: Android.os.Process.killProcess (Android.os.Process.myPid ()); This way you need to add permissions Android.permission.KILL_BACKGROUND_PROCESSES;
or use System.exit (0); exit.

Another method is to use the Activitymanager restartpackage () method; But I tried, and it didn't work.

Others say that the program throws an exception exit, which seriously affects the user experience we are strongly not recommended. There are other ways to do this, such as sending out a broadcast notification of all activity, etc.

One method of personal comparison recommendation is to customize an activity manager to manage all open activity, and then exit all activity through this manager when exiting, and it is validated that this method is feasible and works well.

Here is a simple activity manager code that builds a stack that pushes each open activity onto the stack. When you exit, take it out in turn.

Copy Code code as follows:

public class Myactivitymanager {

private static Myactivitymanager instance;
Private stack<activity> activitystack;//activity Stack

Private Myactivitymanager () {
}
Single case mode
public static Myactivitymanager getinstance () {
if (instance = = null) {
Instance = new Myactivitymanager ();
}
return instance;
}
To push an activity into the stack
public void pushoneactivity (activity actvity) {
if (Activitystack = = null) {
Activitystack = new stack<activity> ();
}
Activitystack.add (actvity);
LOG.D ("Myactivitymanager", "size =" + activitystack.size ());
}
Get the top of the stack activity, advanced out of principle
Public activity getlastactivity () {
return Activitystack.lastelement ();
}
Remove an activity
public void poponeactivity (activity activity) {
if (activitystack!= null && activitystack.size () > 0) {
if (activity!= null) {
Activity.finish ();
Activitystack.remove (activity);
activity = NULL;
}

}
}
Quit all activity
public void finishallactivity () {
if (activitystack!= null) {
while (Activitystack.size () > 0) {
Activity activity = getlastactivity ();
if (activity = null) break;
Poponeactivity (activity);
}
}
}}

In each activity, the OnCreate method calls the push method to push the current activity into the management stack. For example, in the mainactivity:
Myactivitymanager mam = myactivitymanager.getinstance ();
Mam.pushoneactivity (mainactivity.this); The current activity is pushed into the stack. Exit all Activ by calling to exit all activity locations

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.