|
1 | 1 | package com.mrzhang.component.componentlib.router; |
2 | 2 |
|
| 3 | +import android.support.annotation.Nullable; |
| 4 | +import android.text.TextUtils; |
| 5 | + |
3 | 6 | import com.mrzhang.component.componentlib.applicationlike.IApplicationLike; |
4 | 7 |
|
5 | 8 | import java.util.HashMap; |
|
11 | 14 | public class Router { |
12 | 15 |
|
13 | 16 | private HashMap<String, Object> services = new HashMap<>(); |
| 17 | + //注册的组件的集合 |
| 18 | + private static HashMap<String, IApplicationLike> components = new HashMap<>(); |
14 | 19 |
|
15 | 20 | private static volatile Router instance; |
16 | 21 |
|
@@ -49,23 +54,62 @@ public synchronized void removeService(String serviceName) { |
49 | 54 | services.remove(serviceName); |
50 | 55 | } |
51 | 56 |
|
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 | + } |
53 | 90 | try { |
54 | 91 | Class clazz = Class.forName(classname); |
55 | 92 | IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance(); |
56 | 93 | applicationLike.onCreate(); |
| 94 | + components.put(classname, applicationLike); |
57 | 95 | } catch (Exception e) { |
58 | 96 | e.printStackTrace(); |
59 | 97 | } |
60 | 98 | } |
61 | 99 |
|
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; |
69 | 113 | } |
70 | 114 | } |
71 | 115 |
|
|
0 commit comments