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 Android client open source project, primarily for activity management and application exit.
If we exit the program, call Appmanager.appexit (Context) to destroy all the activity instances in the stack.
If we want to be more comfortable with this management class, it is recommended to create a base class for activity, and call the corresponding method in the OnCreate and Ondestory methods, so that we do not need to separate in an activity to add repetitive logic, thin code, such as the following
/** * 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