First, ideas and methods:
First, create a Activitymanager class to hold the activity object.
Returns the Activitymanager object for baseactivity to operate.
All other child activity inherits Baseactivity.
Second, the Code implementation
Activitymanager class:
PackageCom.example.mydemo;Importjava.lang.ref.SoftReference;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map.Entry;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classActivitymanager {/*** Note that this is a singleton mode, where only one object is returned in Baseactivity. Otherwise, each inherited baseactivity will get a new object when the child activity is created. Each new object creates its own hashmap, and the effect is that a hashmap only has one activity, which is obviously not the same as the result we want. * Therefore, you must use the singleton mode*/ Private StaticActivitymanager Activitymanager; Public StaticActivitymanager Getactivitymanager () {if(Activitymanager = =NULL) {Activitymanager=NewActivitymanager (); } returnActivitymanager; } //here, optional. PrivateActivitymanager () {}/*** Task map, used to record activity stack, easy to exit the program (here in order not to affect the system recycling activity, so with a soft reference)*/ Private Finalhashmap<string, softreference<activity>> taskmap =NewHashmap<string, softreference<activity>>(); /*** Add activity to application task map*/ Public Final voidputactivity (Activity atv) {taskmap.put (atv.tostring (),NewSoftreference<activity>(ATV)); LOG.I ("Putactivity", "" "+ATV); } /*** Add activity to application task map*/ Public Final voidremoveactivity (Activity atv) {taskmap.remove (atv.tostring ()); } /*** Clear the application's task stack, which will cause the app to bounce back to the desktop if the program works*/ Public Final voidexit () { for(iterator<entry<string, softreference<activity>>> Iterator =Taskmap. EntrySet (). iterator (); Iterator.hasnext ();) {SoftReference<Activity> activityreference =Iterator.next (). GetValue (); Activity Activity=Activityreference.get (); LOG.I ("Activitylist", "" "+activity); if(Activity! =NULL) {activity.finish (); }} taskmap.clear (); }}
Baseactivity's Code:
PackageCom.example.mydemo;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classBaseactivityextendsActivity {PrivateActivitymanager Manager =Activitymanager.getactivitymanager (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Manager.putactivity ( This); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Manager.removeactivity ( This); } Public voidexit () {manager.exit (); }}
Child activity only needs to inherit the baseactivity.
Exit activity code for all activity:
PackageCom.example.mydemo;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classFragmentsactivityextendsbaseactivity {PrivateButton exit; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_fragment); Exit=(Button) Findviewbyid (r.id.exit); Exit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubexit (); } }); }}
How Android quits all activity at once (upgrade version)