[Android tool] Activity management tool AppManager and androidappmanager

Source: Internet
Author: User

[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 );}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.