Android quits all activity in the most elegant way

Source: Internet
Author: User

On the exit of all activity, the current online more popular way about the following:

① the way of using Activitymanager;

② a way to customize an activity collection class;

③ by means of sending broadcasts;

④ by killing the current application process;

The specific implementation of the above can be self-Google Baidu, here no longer repeat.

But these methods are either less reliable, either restrictive or not elegant enough. Today we're going to be a simple, efficient, elegant way to get out of all the activity.

Principle

First, the principle. We know that activity has four startup modes: Standard (default mode), Singletop, Singletask, SingleInstance. This method needs to use the Singletask startup mode.

If you set an activity's startup mode to Singletask, each time you start this activity, the system checks to see if an instance of this activity already exists in the current task stack. If it already exists, the new instance of the activity is no longer created, but instead the Onnewintent () method of the existing activity is called, and intent is passed to it as a parameter to this method. It will then be placed on top of the stack and removed from all previous activity.

Principle Realization

(we first refer to the first activity into the application as baseactivity). After understanding the basic principle is good, first set the Baseactivity startup mode to Singletask, and rewrite the activity of the Onnewintent () method to end themselves, This activity will then go into other activity for an indefinite number of jumps. When you need to exit, only the current Activity startactivity (This,baseactivity.class) will first end all activity except baseactivity. It then automatically calls Baseactivity's Onnewintent () to end up, so that all the activity is perfectly exited.

Graphical implementation

For ease of understanding, use a concrete implementation process that demonstrates this approach.

Code implementation

First step: Set the Baseactivity startup mode to Singletask

Android:launchmode= "Singletask"

Step two: Rewrite Baseactivity's Onnewintent () method

Declares a static constant that is used as the Tagpublic static final String EXIST = "EXIST" to exit baseactivity; @Overrideprotected void Onnewintent (Intent Intent) {    super.onnewintent (intent);    if (intent! = NULL) {//Determines whether the intent passed when other activity initiates the activity is empty        //Gets the Boolean value of the corresponding tag in the intent        boolean isexist = Intent.getbooleanextra (EXIST, false);        If True then exit this activity        if (isexist) {            this.finish ();}        }    }

Step three: Start baseactivity in activity that needs to exit the app

Intent Intent = new Intent (this,baseactivity.class);//The Boolean value that passes the tag that exits all activity is Trueintent.putextra ( Baseactivity.exist, True);//Start baseactivitystartactivity (intent);

Android quits all activity in the most elegant way

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.