什么是ARouter
ARouter是阿里巴巴推出的一款android界面路由框架
ARouter解决的核心问题是什么
在大型的模块化项目中,一个模块,往往无法直接访问到其它模块中的类,必须通过其它方式来完成模块间的调用
ARouter的核心功能在于,它以路径访问的方式,来取代类直接访问的方式,来实现界面跳转功能,从而达到了模块间代码解耦的目的
引入依赖和启用插件
buildscript {repositories {mavenLocal()maven { url 'https://jitpack.io' }maven { url 'https://maven.aliyun.com/repository/google' }maven { url 'https://maven.aliyun.com/repository/central' }maven { url 'https://maven.aliyun.com/repository/releases' }google()mavenCentral()}dependencies {classpath "com.android.tools.build:gradle:4.2.2"classpath "com.alibaba:arouter-register:1.0.2"}}
apply plugin: 'com.android.application'apply plugin: 'com.alibaba.arouter'android {compileSdkVersion 32defaultConfig {applicationId "com.android.code"minSdkVersion 28targetSdkVersion 32//在build/generated/ap_generated_sources/debug/out目录下生成路由文档javaCompileOptions {annotationProcessorOptions {arguments = [AROUTER_MODULE_NAME: project.getName(), AROUTER_GENERATE_DOC: "enable"]}}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}buildFeatures {viewBinding truedataBinding true}}dependencies {api 'androidx.appcompat:appcompat:1.2.0'api 'com.google.android.material:material:1.3.0'api 'com.alibaba:arouter-api:1.0.2'annotationProcessor 'com.alibaba:arouter-compiler:1.0.2'}
初始化
在Application创建时执行以下代码
package com.android.code;import android.app.Application;import com.alibaba.android.arouter.launcher.ARouter;public class APP extends Application {@Overridepublic void onCreate() {super.onCreate();ARouter.openLog();ARouter.openDebug();ARouter.init(this);}}
在安卓组件间跳转
@Route(path = "/activity/second")public class SecondActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}
ARouter.getInstance().build("/activity/second").navigation();
在安卓组件间传递参数
Postcard postcard = ARouter.getInstance().build("/activity/second");postcard.withString("name", "A");postcard.withInt("age", 18);postcard.navigation();
@Route(path = "/activity/second")public class SecondActivity extends AppCompatActivity {@AutowiredString name;@Autowiredint age;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ARouter.getInstance().inject(this);Log.e("HelloCode", name);}}
在网页中调用安卓组件
通过以下链接,就可以直接访问对应的组件
arouter://m.aliyun.com/activity/second?name=A&age=18
跳转拦截
@Interceptor(priority = 1)public class RouteInterceptor implements IInterceptor {@Overridepublic void process(Postcard postcard, InterceptorCallback callback) {String path = postcard.getPath();if (path.endsWith("/activity/second"))callback.onInterrupt(new RuntimeException("Interrupted"));elsecallback.onContinue(postcard);}@Overridepublic void init(Context context) {Log.e("HelloCode", "RouteInterceptor Init");}}
跳转结果监听
Postcard postcard = ARouter.getInstance().build("/activity/secondXXX");postcard.withString("name", "A");postcard.withInt("age", 18);postcard.navigation(this, new NavigationCallback() {@Overridepublic void onFound(Postcard postcard) {Log.e("HelloCode", "Route Found");}@Overridepublic void onLost(Postcard postcard) {Log.e("HelloCode", "Route Not Found");}@Overridepublic void onArrival(Postcard postcard) {Log.e("HelloCode", "Route Arrival");}@Overridepublic void onInterrupt(Postcard postcard) {Log.e("HelloCode", "Route Interrupt");}});
接口发现和自动装载
ARouter允许根据实现类的路径,自动装载一个接口实例
public interface IPrintService extends IProvider {void print();}
@Route(path = "/service/print/a")public class APrintService implements IPrintService {@Overridepublic void print() {Log.e("HelloCode", "APrintService Print");}@Overridepublic void init(Context context) {}}
@Route(path = "/activity/second")public class SecondActivity extends AppCompatActivity {@Autowired(name = "/service/print/a")IPrintService printService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ARouter.getInstance().inject(this);printService.print();}}
也可以通过ARouter直接实例化一个对象
Postcard postcard = ARouter.getInstance().build("/service/print/a");IPrintService printService = (IPrintService) postcard.navigation();printService.print();
预处理服务
ARouter提供了一个服务类,允许我们在跳转前,做一些预处理工作
注意,这个预处理服务是对所有跳转任务都有效的,无关path
@Route(path = "/pretreatment/global")public class PreService implements PretreatmentService {@Overridepublic void init(Context context) {Log.e("HelloCode", "PreService Init");}@Overridepublic boolean onPretreatment(Context context, Postcard postcard) {Log.e("HelloCode", "PreService PreWork");return true;}}
重定向服务
@Route(path = "/redirect/global")public class RedirectService implements PathReplaceService {@Overridepublic void init(Context context) {Log.e("HelloCode", "RedirectService Init");}@Overridepublic String forString(String path) {boolean inner = path.startsWith("/arouter");if (inner)return path;Log.e("HelloCode", "RedirectService Redirect");return "/activity/second";}@Overridepublic Uri forUri(Uri uri) {return uri;}}