欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?組件化神器之Arouter依賴配置使用

 更新時間:2023年06月29日 14:05:26   作者:沒有了遇見  
這篇文章主要為大家介紹了Android?組件化神器之Arouter依賴配置使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Arouter 支持模塊間的路由、通信、解耦

組件化項(xiàng)目存在各個模塊之間耦合,通信麻煩的問題 ,,為了解決這個問題,阿里巴巴的開發(fā)者就搞出了Arouter這個框架,以解決上述問題.

1.依賴和配置

1.1 Java 環(huán)境配置方案

android {
    compileSdkVersion = 30
    buildToolsVersion = "30.0.3"
    defaultConfig {
        applicationId "com.wu.material"
        ....
        //ARouter 配置
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
       }
dependencies {
   implementation 'com.alibaba:arouter-api:1.5.1'
   annotationProcessor 'com.alibaba:arouter-compiler:1.2.1'
   }
 }

1.2 Kotlin 環(huán)境配置方案

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}
kapt {
    arguments {
        arg("AROUTER_MODULE_NAME", project.getName())
    }
}
android {
    compileSdkVersion = 30
    buildToolsVersion = "30.0.3"
 defaultConfig {
        applicationId "com.wu.material"
      }
dependencies {
    implementation 'com.alibaba:arouter-api:1.5.1'
    kapt 'com.alibaba:arouter-compiler:1.2.1'
}
}

注意:
Kotlin 環(huán)境和 Java 環(huán)境配置不匹配會報(bào)錯 ARouter::There is no route match the path

W/ARouter::: ARouter::There is no route match the path [/xxx/xxx], in group [xxx][ ]"

2. 在Application初始化

    /**
     * 初始化路由
     */
    private fun initArouter() {
        // 這兩行必須寫在init之前,否則這些配置在init過程中將無效
        if (BuildConfig.DEBUG) { 
            // 打印日志
            ARouter.openLog();   
            // 開啟調(diào)試模式(如果在InstantRun模式下運(yùn)行,必須開啟調(diào)試模式!線上版本需要關(guān)閉,否則有安全風(fēng)險)
            ARouter.openDebug(); 
        }
        ARouter.init(this)
    }
    override fun onTerminate() {
        super.onTerminate()
        //阿里router需要釋放
        ARouter.getInstance().destroy()
    }

3.Arouter 使用

3.1 Activity Fragment路由配置以及調(diào)用

// 這里的路徑需要注意的是至少需要有兩級,/xx/xx
//注解
@Route(path = "/arouter/ArouterActivity")
class ArouterActivity:AppCompatActivity() {
...
//獲取數(shù)據(jù)
var key2=getIntent().getStringExtra(key2")
}
// 跳轉(zhuǎn)
ARouter.getInstance().build("/arouter/ArouterActivity")
            .withLong("key1", 1l)
            .withString("key2", "888")
            .withObject("key3", new UserInfo("Jack", "Rose"))
            .navigation();
額外方法:
// 構(gòu)建標(biāo)準(zhǔn)的路由請求
ARouter.getInstance().build("/home/main").navigation();
// 構(gòu)建標(biāo)準(zhǔn)的路由請求,并指定分組
ARouter.getInstance().build("/home/main", "ap").navigation();
// 構(gòu)建標(biāo)準(zhǔn)的路由請求,通過Uri直接解析
Uri uri;
ARouter.getInstance().build(uri).navigation();
// 構(gòu)建標(biāo)準(zhǔn)的路由請求,startActivityForResult
// navigation的第一個參數(shù)必須是Activity,第二個參數(shù)則是RequestCode
ARouter.getInstance().build("/home/main", "ap").navigation(this, 5);
// 直接傳遞Bundle
Bundle params = new Bundle();
ARouter.getInstance()
    .build("/home/main")
    .with(params)
    .navigation();
// 指定Flag
ARouter.getInstance()
    .build("/home/main")
    .withFlags();
    .navigation();
// 獲取Fragment
Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment").navigation();
// 對象傳遞
ARouter.getInstance()
    .withObject("key", new TestObj("Jack", "Rose"))
    .navigation();
// 覺得接口不夠多,可以直接拿出Bundle賦值
ARouter.getInstance()
        .build("/home/main")
        .getExtra();
// 轉(zhuǎn)場動畫(常規(guī)方式)
ARouter.getInstance()
    .build("/test/activity2")
    .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
    .navigation(this);
// 轉(zhuǎn)場動畫(API16+)
ActivityOptionsCompat compat = ActivityOptionsCompat.
    makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
// ps. makeSceneTransitionAnimation 使用共享元素的時候,需要在navigation方法中傳入當(dāng)前Activity
ARouter.getInstance()
    .build("/test/activity2")
    .withOptionsCompat(compat)
    .navigation();
// 使用綠色通道(跳過所有的攔截器)
ARouter.getInstance().build("/home/main").greenChannel().navigation();
// 使用自己的日志工具打印日志
ARouter.setLogger();
// 使用自己提供的線程池
ARouter.setExecutor();

3.2 Arouter 路由跳轉(zhuǎn)的攔截器 IInterceptor

(攔截跳轉(zhuǎn)過程,面向切面編程)

// 比較經(jīng)典的應(yīng)用就是在跳轉(zhuǎn)過程中處理登陸事件,這樣就不需要在目標(biāo)頁重復(fù)做登陸檢查
// 攔截器會在跳轉(zhuǎn)之間執(zhí)行,多個攔截器會按優(yōu)先級順序依次執(zhí)行
@Interceptor(priority = 10, name = "測試攔截器")
class ArouterInterceptor : IInterceptor {
    var mContext: Context? = null
    // 攔截器的初始化,會在sdk初始化的時候調(diào)用該方法,僅會調(diào)用一次
    override fun init(context: Context?) {
        mContext = context
    }
    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        Log.e("傳遞的數(shù)據(jù)", "")
        if (postcard != null) {
            var service = ARouter.getInstance().navigation(UserService::class.java)
            //跳轉(zhuǎn)過程中添加數(shù)據(jù)以及處理數(shù)據(jù)
            postcard!!.extras.putString("uid", service.getUid())
        }
        if (callback != null) {
            //傳遞到頁面
            callback!!.onContinue(postcard);
        }
        // 覺得有問題,中斷路由流程
        // callback.onInterrupt(new RuntimeException("我覺得有點(diǎn)異常"));
        // 以上兩種至少需要調(diào)用其中一種,否則不會繼續(xù)路由
    }
}

3.3 處理跳轉(zhuǎn)結(jié)果

 // 增加整個跳轉(zhuǎn)過程的監(jiān)聽 ARouter.getInstance().build("/rv/MediaPlayerActivity").withString("name", "測試名字?jǐn)?shù)據(jù)")
            .navigation(mContext, object : NavigationCallback {
                override fun onFound(postcard: Postcard?) {
                    Log.e("Arouter", "onFound")
                }
                override fun onLost(postcard: Postcard?) {
                    Log.e("Arouter", "onLost")
                }
                override fun onArrival(postcard: Postcard?) {
                    Log.e("Arouter", "onArrival")
                }
                override fun onInterrupt(postcard: Postcard?) {
                    Log.e("Arouter", "onInterrupt")
                }
            })

3.4 通過依賴注入解耦:服務(wù)管理

/**
 * @author wkq
 *
 * @date 2022年05月17日 16:45
 *
 *@des  創(chuàng)建服務(wù)
 *
 */
interface UserService:IProvider {
    fun getUid():String?
    fun getName():String?
    fun getFace():String?
}
/**
 * @author wkq
 *
 * @date 2022年05月17日 16:46
 *
 *@des  實(shí)現(xiàn)服務(wù)
 *
 */
// 注解
@Route(path = "/service/UserSericice")
class UserServiceImpl:UserService {
    override fun getUid(): String? {
        return nickUid
    }
    override fun getName(): String? {
        return nickName
    }
    override fun getFace(): String? {
        return nickFace
    }
    var nickName=""
    var nickFace=""
    var nickUid=""
    override fun init(context: Context?) {
        nickName="我是測試"
        nickFace="我是頭像"
        nickUid="10010"
    }
}
//調(diào)用IProvider 獲取數(shù)據(jù)
//方法1
 var service = ARouter.getInstance().navigation(UserService::class.java)
 var name= service.getName()
//方法2 
var userServiceImpl =ARouter.getInstance().build("/service/UserSericice").navigation() as UserServiceImpl
var userName = userServiceImpl.getName()

總結(jié)

簡單的調(diào)用了一下Arouter的一些方法,整體功能夠用了,在模塊化架構(gòu)的時候Arouter是個利器,一些高級別使用方式?jīng)]有一一列舉,又興趣的可以去官網(wǎng)看一下,地址放在結(jié)尾

資源

1.Arouter 文檔

2.Demo 源碼

以上就是Android 組件化神器之Arouter依賴配置使用的詳細(xì)內(nèi)容,更多關(guān)于Android 組件化Arouter的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論