First, container-type
Create a global container that stores all the activity and loops through the finish activity when exiting
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.os.Bundle;
public class Baseactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Adding activity to the stack
Atycontainer.getinstance (). addactivity (this);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
End activity& To remove the activity from the stack
Atycontainer.getinstance (). removeactivity (this);
}
}
Class Atycontainer {
Private Atycontainer () {
}
private static Atycontainer instance = new Atycontainer ();
private static list<activity> Activitystack = new arraylist<activity> ();
public static Atycontainer getinstance () {
return instance;
}
public void addactivity (Activity aty) {
Activitystack.add (Aty);
}
public void removeactivity (Activity aty) {
Activitystack.remove (Aty);
}
/**
* End All activity
*/
public void finishallactivity () {
for (int i = 0, size = activitystack.size (); i < size; i++) {
if (null! = Activitystack.get (i)) {
Activitystack.get (i). Finish ();
}
}
Activitystack.clear ();
}
}
This method is relatively straightforward, but you can see that Activitystack holds a strong reference to the activity, which means that when an activity exits unexpectedly, Activitystack does not even release the reference, which leads to memory problems, and then we look at a similar approach , but it's a little bit more elegant.
Second, the broadcast type
By registering a broadcast in baseactivity and sending a broadcast when exiting, finish exits
public class Baseactivity extends Activity {
private static final String exitaction = "Action.exit";
Private Exitreceiver Exitreceiver = new Exitreceiver ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Intentfilter filter = new Intentfilter ();
Filter.addaction (exitaction);
Registerreceiver (exitreceiver, filter);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Unregisterreceiver (Exitreceiver);
}
Class Exitreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
BaseActivity.this.finish ();
}
}
}
Third, process-style
End the application by killing the process of the current application directly, simple and brutal, and with (WU) effect!
Android.os.Process.killProcess (Android.os.Process.myPid ());
System.exit (0);
Activitymanager manager = (Activitymanager) getsystemservice (Activity_service);
Manager.killbackgroundprocesses (Getpackagename ());
These three kinds can achieve the same effect, but in the simulator will pop up unfortunately, XXX has stopped message prompt box, but really can quit the app. Part of the real machine directly failed, only finish the current activity (such as my hand on this millet note, several ROM FW layer of domestic changes too much, use this way to be cautious).
Four, RS elegant type
What is RS type? That is receiver+singletask. We know that activity has four load modes, and Singletask is one of them, after using this mode, when StartActivity, it will first in the current stack to query whether there is an instance of activity, if it exists, then to the top of the stack, and remove the stack from all activity on top of it. We open an app, first a splash page, and then finish off the splash page. Jumps to the home page. Then will be on the main page to jump n times, the period will produce a number of volatile activity, some are destroyed, some reside in the stack, but the bottom of the stack is always our homeactivity. This makes the problem much easier. We can gracefully implement the app's exit in two steps.
1, in Homeactivity Register an exit broadcast, and the second broadcast type, but here only need to register in homeactivity a page.
2, set homeactivity start mode is singletask.
When we need to exit, we only need startactivity (This,homeactivity,class), then send an exit broadcast. The above code first removes all activity on top of the stack from the stack, and then receives the broadcast finish itself homeactivity. Everything OK! No frame, no need to consider the model ROM adaptation. There is no memory problem, it is so elegant, simple!
Five, Singletask revision style
And some of the boys after the exchange, a lot of small partners said the registration of radio slightly trouble, small partners downstairs to propose a simpler way, the idea is very simple,
1, set the mainactivity load mode to Singletask
2, rewrite the Onnewintent method in Mainactivity
3. When you need to exit, add the exit tag in intent
Because many small partners on the source of the demand is more enthusiastic, we here directly in the form of code for everyone to explain this way
The first step is to set the mainactivity load mode to Singletask
Android:launchmode= "Singletask"
The second step overrides the Onnewintent () method
private static final String Tag_exit = "EXIT";
@Override
protected void Onnewintent (Intent Intent) {
Super.onnewintent (Intent);
if (intent! = NULL) {
Boolean isexit = Intent.getbooleanextra (Tag_exit, false);
if (isexit) {
This.finish ();
}
}
}
Step three exit
Intent Intent = new Intent (this,mainactivity.class);
Intent.putextra (Mainactivity.tag_exit, true);
StartActivity (Intent);
VI. Lazy Style
This is simpler and requires only the following two steps
1. Set Mainactivity to Singletask
2. Place exit exit at Mainactivity
We can see a lot of applications are double-click two times the home key to exit the application, is based on the way to achieve, here in the paste how to deal with two consecutive clicks to exit the source code
Private Boolean misexit;
@Override
/**
* Double-click the Back button to exit
*/
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
if (misexit) {
This.finish ();
} else {
Toast.maketext (This, "Press again to exit", Toast.length_short). Show ();
Misexit = true;
New Handler (). postdelayed (New Runnable () {
@Override
public void Run () {
Misexit = false;
}
}, 2000);
}
return true;
}
Return Super.onkeydown (KeyCode, event);
}
6 ways to exit Android apps most gracefully