一、頁面路由基本介紹
1.什麼是頁面路由
映射頁面跳轉關係,包含跳轉相關的URL跳轉及值傳遞、攔截器等功能。
2.爲什麼要使用頁面路由
在原始android開發中,當我們需要進行頁面跳轉時,正常寫法如下:
Intent intent = new Intent(mContext, XXActivity.class);
intent.putExtra("key","value");
startActivity(intent);
Intent intent = new Intent(mContext, XXActivity.class);
intent.putExtra("key","value");
startActivityForResult(intent, 100);
但是上述寫法容易出現以下問題:
1、多人協同開發的時候,大家都去AndroidManifest.xml中定義各種IntentFilter,使用隱式Intent,最終發現AndroidManifest.xml中充斥着各種Schame,各種Path,需要經常解決Path重疊覆蓋、過多的Activity被導出,引發安全風險等問題
2、跳轉過程中無法插手:直接通過Intent的方式跳轉,跳轉過程開發者無法干預,一些面向切面的事情難以實施,比方說登錄、埋點這種非常通用的邏輯,在每個子頁面中判斷又很不合理,畢竟activity已經實例化了
3、跨模塊無法顯式依賴:在App小有規模的時候,我們會對App做水平拆分,按照業務拆分成多個子模塊,之間完全解耦,通過打包流程控制App功能,這樣方便應對大團隊多人協作,互相邏輯不干擾,這時候只能依賴隱式Intent跳轉,書寫麻煩,成功與否難以控制
二、頁面路由框架ARouter的實現
引入流程
步驟一、在MyApplication初始化路由 (容易忽略)
/**
* 初始化路由
*/
private void initRouter() {
Configuration.Builder builder = new Configuration.Builder();
builder.registerModules("app", "ModuleMain", "ModuleLogin");//需要使用路由的模塊
Router.initialize(builder.build());
}
步驟二、在需要使用路由的模塊的build文件添加依賴
javaCompileOptions {
annotationProcessorOptions {
arguments = ["moduleName": project.name]
}
}
api 'com.chenenyu.router:router:1.3.2'
annotationProcessor 'com.chenenyu.router:compiler:1.3.2'
步驟三、用Router進行跳轉
Router.build("com.lyw.modulelogin.login.LoginActivity").go(this);
SplashActivity.this.finish();
在跳轉目地activity也要寫上路徑
這只是實現Router引入的步驟,還有很多其他功能,以後再慢慢完善