Android模块化中数据传递/路由跳转实现示例
作者:左手木亽 时间:2023-06-29 03:12:06
虽然说模块通信、路由协议在Android已经不新鲜了,但是如果脱离了那些优秀的开源库我们从零开始自己造一个库,有时候重复造轮子会让自己对所谓"车"的原理懂得更透彻。
直接上造完的轮子地址: https://github.com/Neacy/NeacyComponent
这个轮子有两个大功能分别是模块通信以及路由跳转:
模块通信
首先,统一声明姿势:
public interface IComponent {
String getName();
void startComponent(ComponentParam param);
}
也就是说,在各自的维护的模块内若想提供一个类供别的模块调用那么需要实现这个 IComponent 类,这样子可以根据面向对象的优势统一管理,所以我们就有了接下来的这么一个个Component类,比如:
@NeacyComponent("app")
public class AppComponent implements IComponent {
@Override
public String getName() {
return "app";
}
@Override
public void startComponent(ComponentParam param) {
Log.w("Jayuchou", "==== Start AppComponent ====");
if (param != null && param.getParam().containsKey("callback")) {
ICallBack callBack = (ICallBack) param.getParam().get("callback");
Map<String, Object> results = new HashMap<>();
results.put("result", "我来自AppComponent");
ComponentParam cp = new ComponentParam(results);
callBack.onComponentBack(cp);
}
}
}
两个地方比较重要:
NeacyComponent 这个注释,主要是为后面的gradle扫描使用
getName() 这个方法返回每个 IComponent 对应的实例key值,方便在不同的模块我们可以根据这个key值找到对应的 IComponent 对象
其次,如何调用呢?
ComponentController.getComponentByName("app").startComponent(null);
是的,只要根据app这个key值我们就能轻易的找到对应的 IComponent 对象,从而执行 startComponent ,这个方法就是你想要在该模块做的逻辑地方。
看上面我们声明的 AppComponent 类,我们在 startComponent 有判断一下传入的参数是否为空,这里直接放了一个伪 Map 类专门用于存放传递的参数。
如何回调结果以及如何获取别的模块的回调结果?
首先你执行了别的模块的 startComponent 方法,在这个方法中你返回的类肯定只有对应的模块能识别,也就是说你在自己模块获取不到别的模块中的类,所以这里使用 ComponentParam 采用key/value的风格存放参数以及回调返回结果,然后看一下下面的代码就能明白答案了。
// 传递参数给IComponent, 可以通过传递回调函数从而得到回调结果
Map<String, Object> p = new HashMap<>();
p.put("callback", new ICallBack() {
@Override
public void onComponentBack(ComponentParam result) {
Log.w("Jayuchou", "==== 运行结果 = " + result.getParam().get("result"));
}
});
ComponentParam cp = new ComponentParam(p);
// 回调结果回去
ICallBack callBack = (ICallBack) param.getParam().get("callback");
Map<String, Object> results = new HashMap<>();
results.put("result", "我来自AppComponent");
ComponentParam cp = new ComponentParam(results);
callBack.onComponentBack(cp);
// 调用的时候传入参数即可
ComponentController.getComponentByName("app").startComponent(cp);
路由跳转
首先,老规矩肯定也是声明一下路由协议(这里只是一个简单的字符串)
@NeacyProtocol("/activity/a")
public class AActivity extends AppCompatActivity
@NeacyProtocol("/activity/b")
public class BActivity extends AppCompatActivity
@NeacyProtocol("/activity/app")
public class MainActivity extends AppCompatActivity
然后调用就是了:
RouterController.startRouter(MainActivity.this, "/activity/a");// 跳转到AActivity
Bundle args = new Bundle();
args.putString("key", "AActivity");
RouterController.startRouter(AActivity.this, "/activity/b", args);// 跳转到BActivity并携带bundle参数
原理
原理就是通过gradle插件结合ASM扫描注解并在编译的时候注入代码,我们先看 * 入成功后的代码结构:
1.模块通信的注入结果
public class ComponentController
{
static
{
registerComponent(new AComponent());
registerComponent(new BComponent());
registerComponent(new AppComponent());
}
private static Map<String, IComponent> components = new HashMap();
static void registerComponent(IComponent component)
{
components.put(component.getName(), component);
}
.
.
.
}
2.路由跳转注入结果
public class RouterController
{
static
{
addRouter("/activity/a", "com.neacy.neacy_a.AActivity");
addRouter("/activity/b", "com.neacy.neacy_b.BActivity");
addRouter("/activity/app", "com.neacy.component.MainActivity");
}
private static Map<String, String> routers = new HashMap();
public static void addRouter(String key, String value)
{
routers.put(key, value);
}
}
3.更多gradle插件的代码查阅 https://github.com/Neacy/NeacyComponent
最后
再次感谢灵感: https://github.com/luckybilly/CC
来源:https://www.jianshu.com/p/e5fdacdd4488
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot 配合 SpringSecurity 实现自动登录功能的代码
![](https://img.aspxhome.com/file/2023/6/57626_0s.png)
SpringBoot项目鉴权的4种方式小结
Java实现的Base64加密算法示例
![](https://img.aspxhome.com/file/2023/8/58618_0s.jpg)
java8中的默认垃圾回收器(GC)
![](https://img.aspxhome.com/file/2023/2/86842_0s.png)
Android实现音量调节的方法
![](https://img.aspxhome.com/file/2023/9/123299_0s.png)
IDEA设置maven修改settings.xml配置文件无法加载仓库的解决方案
![](https://img.aspxhome.com/file/2023/5/66235_0s.png)
Android双击事件拦截方法
Android自定义ViewGroup实现标签浮动效果
![](https://img.aspxhome.com/file/2023/3/138153_0s.jpg)
android实现倒计时功能(开始、暂停、0秒结束)
Java开发Oracle数据库连接JDBC Thin Driver 的三种方法
基于java实现租车管理系统
![](https://img.aspxhome.com/file/2023/8/73038_0s.jpg)
Android仿优酷圆形菜单学习笔记分享
![](https://img.aspxhome.com/file/2023/1/115991_0s.gif)
C#实现Dictionary字典赋值的方法
![](https://img.aspxhome.com/file/2023/3/122693_0s.png)
MybatisPlus多表连接查询的问题及解决方案
![](https://img.aspxhome.com/file/2023/8/60348_0s.png)
JDBC使用游标实现分页查询的方法
ava实现一致性Hash算法
![](https://img.aspxhome.com/file/2023/5/85825_0s.jpg)
Lombok使用@Tolerate实现冲突兼容问题
![](https://img.aspxhome.com/file/2023/4/91104_0s.png)
C# MVC模式中应该怎样区分应用程序逻辑(Controller层)和业务逻辑(Model层)?
C# WPF实现的语音播放自定义控件
![](https://img.aspxhome.com/file/2023/5/122665_0s.gif)
request如何获取body的json数据
![](https://img.aspxhome.com/file/2023/4/61394_0s.jpg)