平時開發中,我們經常用到頁面跳轉功能。之前我一直使用Intent過跳轉
Intent intent = new Intent(A.this, B.class); intent.putExtra("key","value"); startActivity(intent);
最近看到有大牛使用ARouter,專門了解一下,弄個簡單入門的demo
下面的文字粘自Alibaba Open Source
A android router middleware that help app navigating to activities and custom services.
- 支持直接解析標准URL進行跳轉,並自動注入參數到目標頁面中
- 支持多模塊工程使用
- 支持添加多個攔截器,自定義攔截順序
- 支持依賴注入,可單獨作為依賴注入框架使用
- 支持InstantRun
- 支持MultiDex(Google方案)
- 映射關系按組分類、多級管理,按需初始化
- 支持用戶指定全局降級與局部降級策略
- 頁面、攔截器、服務等組件均自動注冊到框架
- 支持多種方式配置轉場動畫
- 支持獲取Fragment
- 完全支持Kotlin以及混編(配置見文末 其他#5)
多個module間解耦,組件化開發,跳轉同一管理
使用步驟:
1. 配置build.gradle:
defaultConfig中添加
//arouter(Android頁面路由框架)
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
dependencies中添加
//arouter(Android頁面路由框架)https://github.com/alibaba/ARouter
compile 'com.alibaba:arouter-api:1.2.2'
annotationProcessor 'com.alibaba:arouter-compiler:1.1.3'
2.所有支持路由的頁面都要添加注解:
@Route(path=MyARouter.MainActivity)

3.在application中初始化SDK:

4.ARouter發起頁面跳轉

Demo鏈接:https://github.com/HeavenDong/ARouterDemo
alibaba開源有更詳細的使用:https://github.com/alibaba/arouter
