使用场景
有的时候我们需要在一个activity1中销毁另一个制定的activity2。或者是在程序的某个位置销毁所有的activity,达到退出整个app的目的
我在网上找到一个做法,考虑的很严谨,在stack里存的不是activity,而是WeakReference。作者的愿意是担心出现有activity被finish之后ActivityMannager却忘了通知的情况,然后就是内存泄露 。。
这里主要是说在系统内存不足杀死Activity时onDestory方法不会被调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| public class FinishActivityManager extends BaseActivity { private FinishActivityManager() { } private static FinishActivityManager sManager; private Stack<WeakReference<Activity>> mActivityStack; public static FinishActivityManager getManager() { if (sManager == null) { synchronized (FinishActivityManager.class) { if (sManager == null) { sManager = new FinishActivityManager(); } } } return sManager; }
public void addActivity(Activity activity) { if (mActivityStack == null) { mActivityStack = new Stack<>(); } mActivityStack.add(new WeakReference<>(activity)); }
public void checkWeakReference() { if (mActivityStack != null) { for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) { WeakReference<Activity> activityReference = it.next(); Activity temp = activityReference.get(); if (temp == null) { it.remove(); } } } }
public Activity currentActivity() { checkWeakReference(); if (mActivityStack != null && !mActivityStack.isEmpty()) { return mActivityStack.lastElement().get(); } return null; }
public void finishActivity() { Activity activity = currentActivity(); if (activity != null) { finishActivity(activity); } }
public void finishActivity(Activity activity) { if (activity != null && mActivityStack != null) { for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) { WeakReference<Activity> activityReference = it.next(); Activity temp = activityReference.get(); if (temp == null) { it.remove(); continue; } if (temp == activity) { it.remove(); } } activity.finish(); } }
public void finishActivity(Class<?> cls) { if (mActivityStack != null) { for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) { WeakReference<Activity> activityReference = it.next(); Activity activity = activityReference.get(); if (activity == null) { it.remove(); continue; } if (activity.getClass().equals(cls)) { it.remove(); activity.finish(); } } } }
public void finishAllActivity() { if (mActivityStack != null) { for (WeakReference<Activity> activityReference : mActivityStack) { Activity activity = activityReference.get(); if (activity != null) { activity.finish(); } } mActivityStack.clear(); } }
public void exitApp() { try { finishAllActivity(); System.exit(0); android.os.Process.killProcess(android.os.Process.myPid()); } catch (Exception e) { e.printStackTrace(); } } }
|
然后是一个BaseActivity类, 重写onCreate和onDestory方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.example.chenlei.test;
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log;
public class BaseActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyActivityManager.addActivity(this); Log.i("有activity新添加", "onCreate: "); }
@Override protected void onDestroy() { Log.i("有activity被销毁", "onDestroy: "+ MyActivityManager.getSize()); MyActivityManager.finishActivity(this); super.onDestroy(); } }
|
接下来所有的activity类都继承自BaseActivity, 然后就可以在制定的activity类中对ActivityManager类进行操作
网上原文:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0629/8124.html