Android activity Management class optimized memory is very convenient

Source: Internet
Author: User

The more activity The project opens, the more memory we have, do we sometimes wonder how many activity,ok we open when I open up a lot of interfaces, and now your problem is solved, only by copying the activity management class into your project, Manage your activity anytime, anywhere, and make you a memory leader! Don't ask me what my name is, please call me Lei Feng! First Look at:



The code Presents:

/** * 2014-6-7 a.m. 10:40:16 */package com.jiub.client.mobile.manager;import Java.util.stack;import android.app.Activity  Import com.jiub.client.mobile.utils.qlog;/** * @author zqy * * */public class Screenmanager {private final static String   Tag=screenmanager.class.getsimplename ();   private static stack<activity> Activitystack;       private static Screenmanager instance;        public static Screenmanager Getscreenmanager () {if (instance = = null) {instance = new Screenmanager ();        } qlog.i (TAG, "Screenmanager create");            return instance;     }/** * Remove the last element from the stack??        */public void popactivity () {Activity activity = currentactivity ();            if (activity! = NULL) {if (!activity.isfinishing ()) {activity.finish ();            } activitystack.removeelement (activity);        activity = NULL;            } qlog.i (TAG, "Screenmanager popactivity"); }/** * Remove the reference from the stackFixed element * @param activity */public void popactivity (activity activity) {if (activity! = null) {Qlo            G.I (TAG, "to delete" +activity.getclass (). Getsimplename ());                if (!activity.isfinishing ()) {activity.finish ();            QLOG.I (TAG, Activity.getclass (). Getsimplename () + "shutting down");            }else{qlog.i (TAG, Activity.getclass (). Getsimplename () + "closed");            } activitystack.removeelement (activity);            QLOG.I (TAG, Activity.getclass (). Getsimplename () + "Remove from stack");        activity = NULL;        } int size=activitystack.size ();    QLOG.I (TAG, "number after deletion" +size); /** * Add new elements to the stack * @param activity */public void pushactivity (activity activity) {if (activitys        Tack = = null) {Activitystack = new stack<activity> ();                } activitystack.addelement (activity);        QLOG.I (TAG, "join" +activity.getclass (). Getsimplename ()); IntSize=activitystack.size ();    QLOG.I (TAG, "number added after" +size);        }/** * Gets the top element of the stack * @return */Public Activity currentactivity () {activity activity = NULL;        try {activity = activitystack.lastelement ();    } catch (Exception e) {} return activity;        }/** * Removes all elements except the specified type * @param CLS */public void Popallactivityexceptone (class<?> cls) {        int size = Activitystack.size ();            for (int i = 0; i < size; i++) {Activity activity = activitystack.get (i);                 if (activity! = null && activity.getclass (). Equals (CLS)) {}else{popactivity (activity);                size--;            i--; }}}/** * End an activity * @param CLS */public void Finishactivitybyclass, according to the given class (        Class<?> CLS) {int size = Activitystack.size (); for (int i = 0; i < size; i++) {ActiviTy activity = activitystack.get (i);                if (activity! = null && activity.getclass (). Equals (CLS)) {popactivity (activity);                size--;            i--; }        }    }    }

Finally, three lines of code are written in the activity OnCreate method:

Protected Screenmanager mscreenmanager;//Initialization
Mscreenmanager = Screenmanager.getscreenmanager (); mscreenmanager.pushactivity (this);
Write this line of code in the OnDestroy method of the activity

Mscreenmanager.popactivity (this);
But if you have baseactivity, you don't have to write in every activity, write the four lines of code in Baseactivity .








Android activity Management class optimized memory is very convenient

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.