The implementation method is to define a collection in application to store all the activity objects, add them to the collection when the activity is created, and finish off all the activity when the program exits.
The steps are as follows:
1. The custom Application,myapplication.java code is as follows:
1 Public classMyApplicationextendsapplication{2 Private StaticArraylist<activity>activitylist;3 Private StaticMyApplication sinstance;4 @Override5 Public voidonCreate () {6 Super. OnCreate ();7activitylist=NewArraylist<activity>();8Sinstance= This;9 }Ten Public StaticMyApplication getinstance () { One returnsinstance; A } - Public voidaddactivity (activity activity) { - Activitylist.add (activity); the } - Public voidexit () { - for(Activity activity:activitylist) { - activity.finish (); + } -System.exit (0); + } A}
2. Define activity base class baseactivity, all activity inheritance baseactivity,activity Add Activity object to application collection when creating, The Baseacivity.java code is as follows:
1 public class baseactivity extends activity{ 2 @Override 3 protected void OnCreate (Bundle Savedinstancestate) { 4 super Span style= "color: #000000;" >.oncreate (savedinstancestate); 5 myapplication.getinstance (). addactivity ( ); 6 7 }
3. In the program exit is called MyApplication exit () method can exit completely, the code is as follows:
Myapplication.getinstance (). exit ();