Android开发中,有时需要同时finish多个Activity,避免用户点击后退按钮时回退到不该呈现的Activity。对于这样的问题,常见的方式是维护一个单例ActivityList,将多个Activity置入此单例列表,需要关闭多个Activity时可遍历该List依次执行finish操作。
代码如下所示:
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
| public class ActivityUtil {
private static List<Activity> activityList;
private static ActivityUtil instance;
private ActivityUtil() {
}
public static ActivityUtil getInstance() {
return null == instance ? new ActivityUtil() : instance;
}
public List<Activity> getActivityList() {
return activityList;
}
//向栈中添加Activity
public void addActivity(Activity activity) {
if (activityList == null) {
activityList = new ArrayList<>();
}
activityList.add(activity);
}
//关闭栈中指定的Activity
public void removeActivity(Activity activity) {
if (activity != null && activityList.contains(activity)) {
activityList.remove(activity);
}
}
//关闭栈顶的Activity
public void removeLastActivity(){
if (activityList.size()>1){
activityList.remove(activityList.size()-1);
}
}
//关闭栈顶topN个Activity
public void finishLastActivites(int n){
int p = activityList.size() - n;
if ( p >= 0 ) {
Iterator<Activity> iterator = activityList.iterator();
int i = 0;
while(iterator.hasNext()){
Activity activity = iterator.next();
if ( i++ >= p ) {
iterator.remove();
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
}
//关闭栈中所有的Activity
public void finishAllActivity() {
if (null != activityList) {
for (Activity activity : activityList) {
if (!activity.isFinishing()) {
activity.finish();
}
}
activityList.clear();
}
}
}
|
这样的代码本身没有问题,但开发时非常容易忘掉此处List中的引用而直接在外部finish,从而造成内存泄露。我们可以在List中使用WeakReference来避免这样的内存泄露。
代码如下所示:
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
| public class ActivityUtil {
private static List<WeakReference<Activity>> activityList;
private static ActivityUtil instance;
private ActivityUtil() {}
public static ActivityUtil getInstance() {
return null == instance ? new ActivityUtil() : instance;
}
public List<WeakReference<Activity>> getActivityList() {
return activityList;
}
public void addActivity(Activity activity) {
if (activityList == null) {
activityList = new ArrayList<>();
}
//使用WeakReference持有Activity
WeakReference<Activity> weakReference = new WeakReference<>(activity);
activityList.add(weakReference);
}
public void removeActivity(Activity activity) {
for (WeakReference<Activity> weakReference : activityList) {
if (weakReference.get() == activity) {
activityList.remove(weakReference);
break;
}
}
}
public void removeLastActivity() {
if (activityList.size() > 1) {
activityList.remove(activityList.size() - 1);
}
}
public void finishLastActivites(int n) {
int p = activityList.size() - n;
if (p >= 0) {
Iterator<WeakReference<Activity>> iterator = activityList.iterator();
int i = 0;
while (iterator.hasNext()) {
Activity activity = iterator.next().get();
if (i++ >= p) {
iterator.remove();
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
}
public void finishAllActivity() {
if (null != activityList) {
for (WeakReference<Activity> weakReference : activityList) {
if (!weakReference.get().isFinishing()) {
weakReference.get().finish();
}
}
activityList.clear();
}
}
}
|