Activity management class, program Exit Tool class

Source: Internet
Author: User

The Activity Management class, which manages all actvity, provides the ability to add activity to the stack, end a specific activity, end all activity, and so on, has the necessary features in project development: Exit function, and exit function usually exits the application in the project. Sign out of the two cases, here for both of these situations are implemented, here to do a record, for later review
  1.  PackageCom.centrvideo.parkapp.util;Importjava.util.concurrent.CopyOnWriteArrayList;Importandroid.app.Activity;ImportAndroid.app.ActivityManager;ImportAndroid.content.Context;Importcom.centrvideo.parkapp.activity.LoginActivity;/*** Activity management class for managing activity, program Exit * @ClassName: AppManager * @Description: *@authorXiaoxiao * @date Modify by 2015-8-17 pm 1:33:27 **/ Public classAppManager {Private StaticCopyonwritearraylist<activity>Mactivitystack; Private StaticAppManager Mappmanager; PrivateAppManager () {}/*** Single Instance*/     Public StaticAppManager getinstance () {if(Mappmanager = =NULL) {Mappmanager=NewAppManager (); }        returnMappmanager; }    /*** Add activity to Stack*/     Public voidaddactivity (activity activity) {if(Mactivitystack = =NULL) {Mactivitystack=NewCopyonwritearraylist<activity>();    } mactivitystack.add (activity); }    /*** End the specified activity*/     Public voidkillactivity (activity activity) {Try {            if(Activity! =NULL) {mactivitystack.remove (activity);                Activity.finish (); Activity=NULL; }        } Catch(Exception e) {e.printstacktrace (); }    }    /*** End activity for specified class name*/     Public voidKillactivity (class<?>CLS) {         for(Activity activity:mactivitystack) {if(Activity.getclass (). Equals (CLS)) {killactivity (activity); }        }    }    /*** End All activity*/     Public voidkillallactivity () { for(inti = 0, size = mactivitystack.size (); i < size; i++) {            if(NULL!=Mactivitystack.get (i))            {Mactivitystack.get (i). Finish ();    }} mactivitystack.clear (); }    /*** End all activity except login*/     Public voidKillallactivitynologin () { for(inti = 0, size = mactivitystack.size (); i < size; i++) {            if(NULL!=Mactivitystack.get (i)) {                if(Mactivitystack.get (i). GetClass ()!=loginactivity.class) {mactivitystack.get (i). Finish ();    }}} mactivitystack.clear (); }    /*** Exit Application*/@SuppressWarnings ("Deprecation")     Public voidAppExit (Context context) {Try{killallactivity (); Activitymanager Activitymgr=(Activitymanager) context. Getsystemservice (Context.activity_service);            Activitymgr.restartpackage (Context.getpackagename ()); System.exit (0); } Catch(Exception e) {e.printstacktrace (); }    }}




From for notes (Wiz)



List of attachments

Activity management class, program Exit Tool class

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.