in the normal development of open three activity, respectively: A, B, C, with the Android device "Back" button, you need to click three times to exit the main interface (that is, three operations to destroy the three activity). So how can you just click on the back one time to destroy the three activity once, the method is very simple, the steps are as follows:(1) Create a Activitycollector.java, which is used as a public class for collecting and destroying activity.
public class Activitycollector {public static list<activity> activities = new arraylist<activity> (); C4/>public static void addactivity (activity activity) { activities.add (activity); } public static void removeactivity (activity activity) { activities.remove (activity); } public static void Finishall () {( Activity activity:activities) { if (!activity.isfinishing ()) { act Ivity.finish ();}}}
(2). Create a Baseactivity.java base class that inherits this class from all activities in the project.
public class Baseactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { //TODO auto-generated method Stub super.oncreate (Savedinstancestate); Activitycollector.addactivity (this); } @Override protected void OnDestroy () { //TODO auto-generated method Stub Super.ondestroy (); Activitycollector.removeactivity (this); }
<span style= "font-family:arial, Helvetica, Sans-serif;" >}</span>
(3) Let A, B, c these three activity respectively to inherit Baseactivity.java. So as long as the activity moves together, or the child activity overrides the OnDestroy () method, The child activity will automatically collect activity to destroy an activity.(4). How to destroy all activity in one place, just call the Finishall () method in Activitycollector.java.For example, invoking the Loginout method on any interface in the app will require a kill multiple activity at a time:
public void Handlemessage (Message msg) { super.handlemessage (msg); Switch (msg.what) {case constantutil.loginout: activitycollector.finishall (); Break
}
}
The code above is simple, but very useful.
How to destroy activity, and how to destroy multiple activity at once