-
Notifications
You must be signed in to change notification settings - Fork 34
/
SUtils.java
169 lines (146 loc) · 4.79 KB
/
SUtils.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.liyi.sutils.utils;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
/**
* @author liyi
*/
public class SUtils {
@SuppressLint("StaticFieldLeak")
private static Application mApplication;
/* 当前处于栈顶的 Activity */
private static WeakReference<Activity> mTopActivityWeakRef;
/* 存储所有存活的 Activity */
private static List<Activity> mActivityList = new LinkedList<Activity>();
/* Activity 的生命周期 */
private static Application.ActivityLifecycleCallbacks mLifecycleCallback = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
// activity 被创建时,向 Activity 列表中添加 activity
mActivityList.add(activity);
setTopActivityWeakRef(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
// activity 被销毁时,从 Activity 列表中移除 activity
mActivityList.remove(activity);
}
};
private SUtils() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 初始化工具类
*
* @param app 应用对象
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void initialize(@NonNull final Application app) {
mApplication = app;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mActivityList == null) mActivityList = new LinkedList<Activity>();
app.registerActivityLifecycleCallbacks(mLifecycleCallback);
}
}
/**
* 获取应用对象
*
* @return Application
*/
public static Application getApp() {
if (mApplication != null) {
return mApplication;
}
throw new NullPointerException("you should initialize first");
}
/**
* 设置栈顶的 Activity
*
* @param activity
*/
private static void setTopActivityWeakRef(Activity activity) {
if (mTopActivityWeakRef == null || mTopActivityWeakRef.get() == null || !mTopActivityWeakRef.get().equals(activity)) {
mTopActivityWeakRef = new WeakReference<>(activity);
}
}
/**
* 获取栈顶的 Activity
*
* @return Activity
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static Activity getTopActivity() {
if (mTopActivityWeakRef != null && mTopActivityWeakRef.get() != null) {
return mTopActivityWeakRef.get();
} else if (mActivityList != null && mActivityList.size() > 0) {
return mActivityList.get(mActivityList.size() - 1);
}
return null;
}
/**
* 获取 Activity 列表
*
* @return List<Activity>
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static List<Activity> getActivityList() {
return mActivityList;
}
/**
* 移除指定的 Activity
*
* @param activity
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void removeActivity(Activity activity) {
if (mActivityList != null) {
if (activity != null && !activity.isFinishing()) {
activity.finish();
}
mActivityList.remove(activity);
}
}
/**
* 移除指定的 Activity
*
* @param cls
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void removeActivity(Class<?> cls) {
if (mActivityList != null) {
for (int i = 0, size = mActivityList.size(); i < size; i++) {
Activity activity = mActivityList.get(i);
if (activity.getClass().getName().equals(cls.getName())) {
if (!activity.isFinishing()) {
activity.finish();
}
mActivityList.remove(i);
break;
}
}
}
}
}