Android App Framework builds------AppManager

Source: Internet
Author: User

Experience a personal summary of some Android app development

When we develop the application. Often there are very many very many activity, at this moment we need an activity stack to help manage the activity's finish and start.

Take the OSC Andrews client case. 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; }/** * Join 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 (reload) */public void finishactivity (CLASS<?

> CLS) {for (baseactivity activity:activitystack) {if (Activity.getclass (). Equals (CLS)) {fi Nishactivity (activity); }}}/** * Closes all activity except for the specified activity assuming that the CLS does not exist in the stack, then all the stacks are emptied * * @param CLS */public void Finishothersactivity (class<?

> 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 operation for the entire application, to see that there is a way to exit the application. Closes the method that specifies activity, closes all activity's methods, and closes all activity except for the specified activity.

So let's talk about the role of this class, first. This class uses a singleton pattern to manage, allowing the entire application to access the activity stack wherever it is. This facilitates the operation of the application.

Like we can define a toast like this.

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

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 we want to return to the processing results. It is not known if 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 Instanceofkeyword to infer if it is the activity we want.

Many other uses. Everyone found it.

Copyright Notice: Original article, reprint please specify from http://kymjs.com/

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