Android 使用ARouter 跳转

    技术2022-07-11  92

    一、页面路由基本介绍

    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引入的步骤,还有很多其他功能,以后再慢慢完善

    Processed: 0.011, SQL: 9