【Android】ARouter新手快速入门

什么是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 32
	
	    defaultConfig {
	
	        applicationId "com.android.code"
	
	        minSdkVersion 28
	        targetSdkVersion 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_8
	        targetCompatibility JavaVersion.VERSION_1_8
	    }
	
	    buildFeatures {
	        viewBinding true
	        dataBinding 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 {
	
	    @Override
	    public void onCreate() {
	        super.onCreate();
	        ARouter.openLog();
	        ARouter.openDebug();
	        ARouter.init(this);
	    }
	}

在安卓组件间跳转


	@Route(path = "/activity/second")
	public class SecondActivity extends AppCompatActivity {
	
	    @Override
	    protected 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 {
	
	    @Autowired
	    String name;
	
	    @Autowired
	    int age;
	
	    @Override
	    protected 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 {
	
	    @Override
	    public void process(Postcard postcard, InterceptorCallback callback) {
	        String path = postcard.getPath();
	        if (path.endsWith("/activity/second"))
	            callback.onInterrupt(new RuntimeException("Interrupted"));
	        else
	            callback.onContinue(postcard);
	    }
	
	    @Override
	    public 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() {
	    @Override
	    public void onFound(Postcard postcard) {
	        Log.e("HelloCode", "Route Found");
	    }
	
	    @Override
	    public void onLost(Postcard postcard) {
	        Log.e("HelloCode", "Route Not Found");
	    }
	
	    @Override
	    public void onArrival(Postcard postcard) {
	        Log.e("HelloCode", "Route Arrival");
	    }
	
	    @Override
	    public 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 {
	
	    @Override
	    public void print() {
	        Log.e("HelloCode", "APrintService Print");
	    }
	
	    @Override
	    public void init(Context context) {
	
	    }
	}


	@Route(path = "/activity/second")
	public class SecondActivity extends AppCompatActivity {
	
	    @Autowired(name = "/service/print/a")
	    IPrintService printService;
	
	    @Override
	    protected 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 {
	
	    @Override
	    public void init(Context context) {
	        Log.e("HelloCode", "PreService Init");
	    }
	
	    @Override
	    public boolean onPretreatment(Context context, Postcard postcard) {
	        Log.e("HelloCode", "PreService PreWork");
	        return true;
	    }
	}

重定向服务


	@Route(path = "/redirect/global")
	public class RedirectService implements PathReplaceService {
	
	    @Override
	    public void init(Context context) {
	        Log.e("HelloCode", "RedirectService Init");
	    }
	
	    @Override
	    public String forString(String path) {
	        boolean inner = path.startsWith("/arouter");
	        if (inner)
	            return path;
	        Log.e("HelloCode", "RedirectService Redirect");
	        return "/activity/second";
	    }
	
	    @Override
	    public Uri forUri(Uri uri) {
	        return uri;
	    }
	}

你可能感兴趣的:(android,ARouter,Android,界面路由)