• 【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"
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    
    	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'
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    初始化

    在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);
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在安卓组件间跳转

    
    	@Route(path = "/activity/second")
    	public class SecondActivity extends AppCompatActivity {
    	
    	    @Override
    	    protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    
    	ARouter.getInstance().build("/activity/second").navigation();
    
    
    • 1
    • 2
    • 3

    在安卓组件间传递参数

    
    	Postcard postcard = ARouter.getInstance().build("/activity/second");
    	postcard.withString("name", "A");
    	postcard.withInt("age", 18);
    	postcard.navigation();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    	@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);
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在网页中调用安卓组件

    通过以下链接,就可以直接访问对应的组件

    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");
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    跳转结果监听

    
    	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");
    	    }
    	});
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    接口发现和自动装载

    ARouter允许根据实现类的路径,自动装载一个接口实例

    
    	public interface IPrintService extends IProvider {
    	
    	    void print();
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    	@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) {
    	
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    
    	@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();
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    也可以通过ARouter直接实例化一个对象

    
    	Postcard postcard = ARouter.getInstance().build("/service/print/a");
    	IPrintService printService = (IPrintService) postcard.navigation();
    	printService.print();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    预处理服务

    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;
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    重定向服务

    
    	@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;
    	    }
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    uniapp小程序长按识别关注公众号
    vim工具的使用
    如何将图片识别为可编辑的Word文件
    Image does NOT change color when selecting it in tiptap
    基于JAVA物业后台管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    淘宝(tmall)店铺旗舰店商品数据分析接口代码教程
    机器学习--Transformer 1
    综述---知识蒸馏
    Javascript笔记
    Mysql基础与高级汇总
  • 原文地址:https://blog.csdn.net/u013718730/article/details/132559274