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