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 tag to exit Baseactivity Public Static FinalString EXIST = "EXIST"; @Overrideprotected voidonnewintent (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 BooleanIsexist = Intent.getbooleanextra (EXIST,false); //If it is true, exit the activity if(isexist) { This. Finish (); } }}
Step three: Start baseactivity in activity that needs to exit the app
New Intent (This, baseactivity. Class); // true to pass the tag that exits all activity for the corresponding Boolean value true ); // start baseactivitystartactivity (intent);
Caobotao Source: http://www.cnblogs.com/caobotao/p/5127645.html This copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to give the original link, Otherwise, the right to pursue legal liability is retained.
Android exits all activity in the most elegant way