Android App Framework builds------AppManager

Source: Internet
Author: User

Personal summary of some of the experience of Android application development

When we develop our applications, we often have a lot of activity, and at this point we need an activity stack to help manage the activity's finish and start.

Take the OSC Android client as an example, the code uses a stack<activity> to save all the Activity.

/** * Application Activity Management class: For activity management and application exit * * @author KYMJS * @version 1.0 * @created 2013-11-24 */public class Appmanage    R {private static stack<baseactivity> activitystack;     private static AppManager instance;        Private AppManager () {}/** * Single instance, UI does not need to consider multithreading synchronization issues */public static AppManager Getappmanager () {        if (instance = = null) {instance = new AppManager ();    } return instance; }/** * Add activity to stack */public void addactivity (baseactivity activity) {if (Activitystack = = null)        {activitystack = new stack<baseactivity> ();    } activitystack.add (activity); }/** * Gets the current activity (top activity) */public baseactivity currentactivity () {if (Activitystack = = nul L | |        Activitystack.isempty ()) {return null;        } baseactivity activity = Activitystack.lastelement ();    return activity; }/** * Get current activity (top of stack activity) not foundTo return NULL */public baseactivity findactivity (class<?> cls) {baseactivity activity = null;                for (baseactivity aty:activitystack) {if (Aty.getclass (). Equals (CLS)) {activity = Aty;            Break    }} return activity; }/** * Ends Current activity (top of stack activity) */public void finishactivity () {baseactivity activity = Activitys        Tack.lastelement ();    Finishactivity (activity);             }/** * Ends the specified activity (overload) */public void finishactivity (activity activity) {if (activity! = NULL) {            Activitystack.remove (activity);            Activity.finish ();        activity = NULL;  }}/** * ends the specified activity (overload) */public void finishactivity (class<?> cls) {for (baseactivity            Activity:activitystack) {if (Activity.getclass (). Equals (CLS)) {finishactivity (activity); }}}/** * offAll activity except the specified activity if the CLS does not exist in the stack, the stack is emptied * * @param cls/public void finishothersactivity (CLASS&LT;?). > CLS) {for (baseactivity activity:activitystack) {if (! (            Activity.getclass (). Equals (CLS))) {finishactivity (activity); }}}/** * End all activity */public void finishallactivity () {for (int i = 0, size = Activ Itystack.size (); i < size;            i++) {if (Null! = Activitystack.get (i)) {activitystack.get (i). Finish ();    }} activitystack.clear ();            }/** * Application exits */public void AppExit (context context) {try {finishallactivity (); Activitymanager activitymgr = (activitymanager) context. Getsystemservice (Context.activity_servi            CE);            Activitymgr.killbackgroundprocesses (Context.getpackagename ());        System.exit (0); } catch (Exception e) {System.exiT (0); }    }}

Here is the activity for the entire application, you can see that there is a way to exit the application, close the method of assigning activity, close all activity, and close all activity except for the specified activity.

So let's talk about the role of this class, first of all, this class uses a singleton mode to manage, so that the entire application can access the activity stack anywhere, which facilitates the operation of the application.

For example, we can define a toast like this

public static showmessage (String msg) {    toast.maketext (Appmanager.getappmanager (). CurrentActivity (), MSG, Toast.length_short). Show ();}

As you can see, we define a toast that can be used globally, no longer constrained by the context, but you need to first make sure that your app is not destroyed by the system before using it.

For example, we sometimes do business in a service, and then want to return to the processing results, but do not know whether the activity is still there (it may have been closed by the user), At this point, you can use the activity stack to get the activity at the top of the current stack by instanceof keyword to determine if it is the activity we want.

More usage, let's dig it out.

Android App Framework builds------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.