Reprinted from: http://www.cnblogs.com/qingblog/archive/2012/06/08/2541790.html
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.
The Exitapplication class code is as follows:
Package ... utils;
Import java.util.LinkedList;
Import java.util.List;
Import android.app.Activity;
Import android.app.Application;
public class Exitapplication extends application
{
Private list<activity> activitylist = new linkedlist<activity> ();
Private volatile static exitapplication instance;
Private Exitapplication ()
{
}
Get a unique exitapplication instance in singleton mode
public static exitapplication getinstance ()
{
if (null = = instance)
{
Synchronized (Exitapplication.class)
{
if (null = = instance)
{
Instance = new Exitapplication ();
}
}
}
return instance;
}
Adding 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);
}
}
Android completely quits the app