Android completely quits the app

Source: Internet
Author: User

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

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.