-
Notifications
You must be signed in to change notification settings - Fork 25
/
ReactActivityManager.java
83 lines (70 loc) · 2.41 KB
/
ReactActivityManager.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
package com.rnprojectplayground;
import android.app.Activity;
import android.text.TextUtils;
import java.util.Stack;
public class ReactActivityManager {
// 管理 RN Activity 的栈结构
private static Stack<Activity> activityStack = new Stack<>();
private ReactActivityManager() {
throw new IllegalStateException("不允许创建实例");
}
/**
* RN Activity 入栈
* @param activity 新的 RN Activity
*/
public static void pushActivity(Activity activity) {
if (activity != null) {
activityStack.push(activity);
}
}
/**
* 获取最顶层的 RN Activity
* @return RN Activity 实例
*/
public static Activity topActivity() {
if (activityStack.empty()) return null;
return activityStack.peek();
}
/**
* 移除指定的 RN Activity
* @param activity RN Activity 实例
*/
public static void removeActivity(Activity activity) {
if (activityStack.empty()) return;
activityStack.removeElement(activity);
}
/**
* 返回到指定的 RN Activity
* @param routeName 对应的 RN 界面名称
* @param animated 是否开启动画(适配iOS?)
*/
public static void popTo(String routeName, Boolean animated) {
if (activityStack.empty() || TextUtils.isEmpty(routeName)) return;
for (int i = activityStack.size() - 1; i >= 0; i--) {
Activity activity = activityStack.get(i);
if (activity instanceof RNPayloadActivity) {
RNPayloadActivity rnPayloadActivity = (RNPayloadActivity)activity;
if (rnPayloadActivity == null) continue;
if (rnPayloadActivity.viewName.equals(routeName)) return;
if (!rnPayloadActivity.isFinishing()) {
rnPayloadActivity.finish();
}
}
}
}
/**
* 返回到最顶部 RN Activity
* @param animated 是否开启动画(适配iOS?)
*/
public static void popToRoot(Boolean animated) {
if (activityStack.empty()) return;
for (int i = activityStack.size() - 1; i >= 0; i--) {
Activity activity = activityStack.get(i);
if (activity == null) continue;
if (activity instanceof MainActivity) return;
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}