"Android Tool class" Activity management tool class AppManager

Source: Internet
Author: User

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&LT;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&lt ; 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

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.