Activity Management Class AppManager

Source: Internet
Author: User

Program launches first interface class: Net.oschina.app.AppStart
Function Description: A picture
Code Detail Description: An animated effect of transparency that automatically launches new activity when the effect animation is completed (Main)

Basic Baseactivity class, inherited from activity
Function Description: Add activity to stack separately in OnCreate method, remove activity in OnDestroy method
Code Detail Description: Provides a singleton class AppManager, the creation of activity into the stack for management, to provide the addition of activity, remove the current activity, remove the specified activity, remove all the activity, Get the current activity and exit the program. This tool class is well suited for use as a development, with the following specific code:

Import Java.util.stack;import Android.app.activity;import Android.app.activitymanager;import android.content.context;/** * Application Activity Management class: For activity management and application exit * @author Liux (http://my.oschina.net/liux) * @    Version 1.0 * @created the 2012-3-21 */public class AppManager {private static stack<activity> activitystack;        private static AppManager instance;            Private AppManager () {}/** * 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); }/** * Gets the current activity (last pressed in the stack) */Public Activity currentactivity () {activity Activity=activitystac        K.lastelement ();    return activity; }/** * Ends the current activity (last pressed 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 of the specified class name */public void finishactivity (class<?> cls) {for (Activity Activit            Y:activitystack) {if (Activity.getclass (). Equals (CLS)) {finishactivity (activity); }}}/** * End all activity */public void finishallactivity () {for (int i = 0, size = Activi Tystack.size (); i < size;            i++) {if (Null! = Activitystack.get (i)) {activitystack.get (i). Finish ();    }} activitystack.clear (); }/** * Exit application */@SuppressWarnings ("deprecation") public void AppExit (context context) {Try {finishallactivity ();            Activitymanager activitymgr= (Activitymanager) Context.getsystemservice (Context.activity_service);            Activitymgr.restartpackage (Context.getpackagename ());        System.exit (0); } catch (Exception e) {}}}

fragment management of imitation activity management writing
/** * @author lee0000 * @version 1 * @time October 7 * */public class Framanager {private static stack<fragment> fra    Gmentstack;    private static Framanager instance;    Private static Final Object obj = new Object (); Private Framanager () {}/** * Single instance */public static Framanager Getframanager () {if (instance = = NULL) {synchronized (obj) {if (instance = = null) {instance = new Framanage                R ();    }}} return instance; }/** * Add Fragment to Stack */public static void Pushfragment (fragmentactivity fact, Fragment Fragment, int l        Ayoutid, String ... title) {if (fragmentstack==null) {fragmentstack=new stack<fragment> ();        } fragmentstack.push (fragment);                Fragmenttransaction ft = Fact.getsupportfragmentmanager (). BeginTransaction ();        Ft.replace (LayoutID, fragment);    if (Fragmentstack.size () > 1) {        Ft.addtobackstack (NULL);    } ft.commit (); }/** * Popup Fragment */public static Fragment Popfragment (fragmentactivity fact) {Fragment Fragme        NT = Fragmentstack.pop ();        Fragmenttransaction ft = Fact.getsupportfragmentmanager (). BeginTransaction ();        Ft.remove (fragment);        Ft.commit ();    return fragment;        }/*** * POPs all fragment in the specified activity * */public static void Finishfragment (fragmentactivity fact) {            for (int i = 0, size = fragmentstack.size (), I < size; i++) {Fragment stackfragment = Fragmentstack.get (i);                if (null! = stackfragment) {Fragment Fragment = stackfragment; if (fact = = Fragment.getactivity ()) {fragmenttransaction ft = Fragment.getfragmentmanager (). BeginTrans                    Action ();                    Ft.remove (fragment);                    Fragmentstack.remove (i);                Ft.commit ();  }            }      }}/** * Clears all items in the stack * */public static void Clearstack () {if (Fragmentstack! = null &        & Fragmentstack.isempty ()) {fragmentstack.clear (); }    }}

Activity Management 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.