Skip to content

Commit d6e6d27

Browse files
author
mrzhang
committed
fix bug in unregisterComponent
1 parent c83c5cb commit d6e6d27

File tree

1 file changed

+52
-8
lines changed
  • componentlib/src/main/java/com/mrzhang/component/componentlib/router

1 file changed

+52
-8
lines changed

componentlib/src/main/java/com/mrzhang/component/componentlib/router/Router.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.mrzhang.component.componentlib.router;
22

3+
import android.support.annotation.Nullable;
4+
import android.text.TextUtils;
5+
36
import com.mrzhang.component.componentlib.applicationlike.IApplicationLike;
47

58
import java.util.HashMap;
@@ -11,6 +14,8 @@
1114
public class Router {
1215

1316
private HashMap<String, Object> services = new HashMap<>();
17+
//注册的组件的集合
18+
private static HashMap<String, IApplicationLike> components = new HashMap<>();
1419

1520
private static volatile Router instance;
1621

@@ -49,23 +54,62 @@ public synchronized void removeService(String serviceName) {
4954
services.remove(serviceName);
5055
}
5156

52-
public static void registerComponent(String classname) {
57+
// public static void registerComponent(String classname) {
58+
// try {
59+
// Class clazz = Class.forName(classname);
60+
// IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
61+
// applicationLike.onCreate();
62+
// } catch (Exception e) {
63+
// e.printStackTrace();
64+
// }
65+
// }
66+
//
67+
// public static void unregisterComponent(String classname) {
68+
// try {
69+
// Class clazz = Class.forName(classname);
70+
// IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
71+
// applicationLike.onStop();
72+
// } catch (Exception e) {
73+
// e.printStackTrace();
74+
// }
75+
// }
76+
77+
78+
/**
79+
* 注册组件
80+
*
81+
* @param classname 组件名
82+
*/
83+
public static void registerComponent(@Nullable String classname) {
84+
if (TextUtils.isEmpty(classname)) {
85+
return;
86+
}
87+
if (components.keySet().contains(classname)) {
88+
return;
89+
}
5390
try {
5491
Class clazz = Class.forName(classname);
5592
IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
5693
applicationLike.onCreate();
94+
components.put(classname, applicationLike);
5795
} catch (Exception e) {
5896
e.printStackTrace();
5997
}
6098
}
6199

62-
public static void unregisterComponent(String classname) {
63-
try {
64-
Class clazz = Class.forName(classname);
65-
IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
66-
applicationLike.onStop();
67-
} catch (Exception e) {
68-
e.printStackTrace();
100+
/**
101+
* 反注册组件
102+
*
103+
* @param classname 组件名
104+
*/
105+
public static void unregisterComponent(@Nullable String classname) {
106+
if (TextUtils.isEmpty(classname)) {
107+
return;
108+
}
109+
if (components.keySet().contains(classname)) {
110+
components.get(classname).onStop();
111+
components.remove(classname);
112+
return;
69113
}
70114
}
71115

0 commit comments

Comments
 (0)