Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
Import Java.util.stack;import Android.app.activity;import Android.app.activitymanager;import android.content.context;/** * * @ClassName: Com.qust.myutils.AppManager * @Description: Activity Management class: for managing activity and exiting Programs * @author Zhaokaiqiang * @date 2014-11-20 PM 4:53:33 * */public class AppManager {//activity stack private static STACK<ACTI Vity> activitystack;//Singleton mode private static AppManager instance;private AppManager () {}/** * Single instance */public static Appmana Ger Getappmanager () {if (instance = = null) {instance = new AppManager ();} return instance;} /** * Add activity to stack */public void addactivity (activity activity) {if (Activitystack = = null) {Activitystack = new stack< ; Activity> ();} Activitystack.add (activity);} /** * Gets the current activity (last pressed in the stack) */public activity currentactivity () {Activity activity = activitystack.lastelement (); return activity;} /** * Ends the current activity (last pressed in the stack) */public void finishactivity () {Activity activity = activitystack.lastelement (); Finishactivity (activity);} /** * End of specified Activity */public void finishactivity (activity activity) {if (activity! = null) {Activitystack.remove (activity); Activity.finish (); activity = NULL;}} /** * Ends the activity */public void Finishactivity (class<?> cls) {for (activity Activity:activitystack) {if (Activ) of the specified class name Ity.getclass (). Equals (CLS)) {finishactivity (activity);}}} /** * End all activity */public void finishallactivity () {for (int i = 0; i < activitystack.size (); i++) {if (null! = Activ Itystack.get (i)) {activitystack.get (i). Finish ();}} Activitystack.clear ();} /** * Exit application */public void AppExit (context context) {try {finishallactivity (); Activitymanager activitymgr = (activitymanager) context.getsystemservice (Context.activity_service); Activitymgr.killbackgroundprocesses (Context.getpackagename ()); System.exit (0);} catch (Exception e) {}}}
This class is extracted from Oschina's androidclient Open source project, which is used primarily for activity management and application exit.
Assuming that we exit the program, invoking Appmanager.appexit (Context) will be able to destroy all the activity instances in the stack.
Suppose we are more comfortable with this management class, it is recommended to create a base class of activity, and invoke the corresponding method inside the OnCreate and Ondestory methods, so that we do not need to add the repeated logic in an activity alone. Thin code. For example, like this
/** * Initializes the context and adds the current activity to the stack for easy management */@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Add activity to Stack Appmanager.getappmanager (). addactivity (this);} @Overrideprotected void OnDestroy () {Super.ondestroy ();//End activity& Remove Appmanager.getappmanager () from the stack. Finishactivity (this);}
"Android Tool class" Activity management tool class AppManager