router路由框架
什么是路由?说简单点就是映射页面跳转关系的,当然它也包含跳转相关的一切功能。
前言
网上大片的路由框架实在太多了,实现的方式都大同小异,通过注解实现路由表;但是在多module开发的时候怎么合并路由表,不同的框架有着自己的解决方案。
arouter:通过类查找进行合并路由表。
activityrouter:通过注解进行路由表合并。
路由框架的意义
android系统已经给我们提供了api来做页面跳转,比如startactivity,为什么还需要路由框架呢?我们来简单分析下路由框架存在的意义:
特性
router有哪些特性或者优点呢?
开始
对于路由表的合并我采用了activityrouter的方案,进行注解合并路由表。
1.源码:
if (!hasmodules && !hasmodule){ //普通app createrapprouterhelper(); } if (hasmodule){ //保存每个module中的路由表 creatermodulehelper(modulename); } if (hasmodules){ //合并每个module中的路由表 createrrouterhelper(modulenames); }
2.用法:
app:
@module("app") @modules({"app","module"}) public class app extends application{ ... }
module:
@module("module") @path("module") public class moduleactivity extends appcompatactivity { ... }
3.备注:非多module开发以上直接省略直接初始化路由即可
初始化
router.init(this.getapplicationcontext());//bixu router.debug(true);//开启debug模式
页面跳转
程序内部界面之间跳转 通过 path 查找路由表中 对应的activity 进行页面跳转。
外部跳转程序内部 通过 uri 拦截 activity 对uri进行拦截处理并最终通过路由表查找 path进行跳转。
1.用法
基本用法
router.getinstance().path("second"); router.getinstance().action(mediastore.action_image_capture); router.getinstance().uri("https://www.waws.top/module?id=2&name=haha");
okhttp式用法
//同步request request = new request.builder(this).path("second").build(); response response = router.getinstance().newcall(request).execute(); //异步request = new request.builder(this) .path("second") .responsecode(100) .resultcallback(new resultcallback() { @override public void next(int resultcode, intent data) { routerlog.d("resultcode:"+resultcode+"\ndata:"+data.getstringextra("tag")); } }) .addoption(activityoptions.makescenetransitionanimation(this,bt,"share").tobundle()) .build(); router.getinstance().skipintecepter().newcall(request) .enqueue(new routercallback() { @override public void next(response response) { routerlog.d(response.tostring()); } });
拦截activity onactivityresult 使用了代理fragment进行动态拦截,有兴趣的可以看源码,地址在下边
拦截器
支持全局拦截并支持降级操作
router.addintecepter(new routerintecepter() { @override public request chain(request request) { //todo: return request; } @override public void onlost(string msg) { //todo: log.d("app", "onlost: "+msg); } @override public void onsuccess() { //todo: log.d("app", "onsuccess: "); } });
外部跳转app内部进行如下配置
<activity android:name=".dispatcheractivity"> <intent-filter> <action android:name="android.intent.action.view"/> <category android:name="android.intent.category.browsable"/> <category android:name="android.intent.category.default"/> <!-- start --> <!-- 修改如下data信息即可 --> <data android:scheme="http" android:host="www.waws.top"/> <data android:scheme="https" android:host="www.waws.top"/> <data android:scheme="router" android:host="www.waws.top"/> <!-- end --> </intent-filter></activity>
源码地址:https://github.com/waws80/router
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
android RecyclerView checkbox复用解决
Android Q Input (1) -- InputManagerService启动
android -- ndk (stack corruption detected)
网友评论