[Android tool] Activity management tool AppManager and androidappmanager
Reprinted please indicate the 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: used to manage the Activity and exit the Program * @ author zhaokaiqiang * @ date 2014-11-20 4:53:33 **/public class AppManager {// Activity Stack private static Stack <Activity> activityStack; // Singleton mode private static AppManager instance; private Ap PManager () {}/*** Single instance */public static AppManager 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);}/*** get the current Activity (the last pushed in the stack) */public Activity currentActivity () {Activity activity = activityStack. LastElement (); return activity;}/*** ends the current Activity (the last pushed in the stack) */public void finishActivity () {Activity activity = activityStack. lastElement (); finishActivity (activity);}/*** ends the specified Activity */public void finishActivity (Activity activity) {if (activity! = Null) {activityStack. remove (activity); activity. finish (); activity = null;}/*** ends the Activity with the specified Class name */public void finishActivity (Class <?> Cls) {for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {finishActivity (activity) ;}}/ *** ends all activities */public void finishAllActivity () {for (int I = 0; I <activityStack. size (); I ++) {if (null! = ActivityStack. 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 the open-source project of the android client of oschina and is mainly used for Activity management and application exit.
If we call AppManager. AppExit (Context) When exiting the program, all the Activity instances in the stack can be destroyed.
If we want to use this management class more conveniently, we recommend that you create a base class for the Activity and call the corresponding method in the onCreate and onDestory methods, in this way, we do not need to add duplicate logic in an Activity to streamline the code, such as the following:
/*** Initialize the context and add the current Activity to the stack to facilitate management */@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // Add the Activity to the stack AppManager. getAppManager (). addActivity (this) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); // end Activity & remove AppManager from Stack. getAppManager (). finishActivity (this );}