ARouter的集成使用

ARouter的集成使用,第1张

ARouter的集成使用

1.这里区分Java和Kotlin,两种方式引用方式略有不同

在app的build.gradle的defaultConfig中配置

android{

        defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }


        //  Java  ARouter配置
//        javaCompileOptions {annotationProcessorOptions {arguments = [AROUTER_MODULE_NAME: project.getName()]}}

    //  Kotlin  ARouter配置
        kapt {arguments {arg("AROUTER_MODULE_NAME", project.getName())}}
        
    }
}

引入依赖:

//   Java ARouter依赖
//    compile 'com.alibaba:arouter-api:1.5.2'
//    annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'
//   Kotlin ARouter依赖
    implementation "com.alibaba:arouter-api:1.5.2"
    kapt "com.alibaba:arouter-compiler:1.5.2"

另外:如果是Kotlin项目 需要添加plug:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    //如果是Kotlin项目 添加以下Pluging
    id 'kotlin-kapt'
    id 'kotlin-android-extensions'
}

这里我放一下我的目录结构

 主要是从MainActivity跳转到不同的Activity

 下面是代码部分:

1.统一的路径

package com.example.myapplication.aaa

object RouterPathCons {
    //路径可以随便配置 但是必须以/开头   一般最好两级/ /   跳转时候build()括号中的路径和目标Activity的@Route(path="路径")必须一致   
    const val  RouterActivity1: String  = "/aaa/a1";
    const val  RouterActivity2: String  = "/bbb/RouterActivity2";
    const val  RouterActivity3: String  = "/ccc/ccc1";
    const val  RouterActivity4: String  = "/ddd/ddd2";
}

2.application中初始化

package com.example.myapplication

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter

class Apps: Application(){
    //Arouter调试开关
    private val isDebugArouter : Boolean =true;
    override fun onCreate() {
        super.onCreate()
        if (isDebugArouter) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }
}

 3.MainActivity中的跳转

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.alibaba.android.arouter.launcher.ARouter
import com.example.myapplication.aaa.RouterPathCons

class MainActivity : AppCompatActivity(),View.onClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        setContentView(R.layout.activity_main)
        val btr1= findViewById

4.其他的类  

JavaRouter1Activity

package com.example.myapplication.aaa;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.myapplication.R;

@Route(path = RouterPathCons.RouterActivity3)
public class JavaRouter1Activity extends Activity {
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route3);
    }
}
JavaRouter2Activity
package com.example.myapplication.aaa;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.myapplication.R;

import kotlin.jvm.JvmField;

@Route(path = RouterPathCons.RouterActivity4)
public class JavaRouter2Activity extends Activity {
    @Autowired(name = "p_char")
    char cuchar;
    @Autowired(name = "p_float")
    float a;
    @Autowired(name = "p_double")
    double b;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        setContentView(R.layout.activity_route4);
        Log.e("======",cuchar+","+"float:"+a+",double:"+b);
    }
}
KtRouter1Activity
package com.example.myapplication.aaa

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.myapplication.R

@Route(path = RouterPathCons.RouterActivity1)
 class KtRouter1Activity :AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_route1)
        ARouter.getInstance().inject(this)

    }
}

 RouterActivity2

package com.example.myapplication.aaa

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.myapplication.R



@Route(path = RouterPathCons.RouterActivity2)
 class RouterActivity2 :AppCompatActivity() {

    @Autowired(name="p_byte")
    @JvmField
    var  mbyte:Byte=0
    @Autowired(name="p_float")
    @JvmField
    var  mfloat:Float=0.0f
    @Autowired(name="p_double")
    @JvmField
    var  mdouble:Double=-1.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_route2)

        ARouter.getInstance().inject(this)
        Log.e("KtRouter2","Byte:$mbyte ,Float:$mfloat ,Double:$mdouble")
    }
}

需要注意的是:使用ARouter跳转  

1.目标Activity必须配置

@Route(path="路径") 

2.如果目标Activity需要接收携带过来的参数除了要配置

ARouter.getInstance().inject(this)
@Autowired(name="需要与传递过来的Key一致") 

如果是Kotlin类的Activity  需要加上@JvmField 使变量非私有,否则编译会报错

ARouter::Compiler An exception is encountered, [The inject fields CAN NOT BE 'private'!!! please check field [mdouble] in class [com.example.myapplication.aaa.RouterActivity2]]

 The inject fields CAN NOT BE 'private'!!! 

 我就匆匆忙忙引入试了一下   还有其他没介绍的 之后有时间再写

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4656673.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存