"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 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

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.