android源码、开源框架解析

组件化ARoute使用总结

2020-08-18  本文已影响0人  吃饱了就送

ARouter

项目结构

app为壳,commonlib为公共组件库,chat,home等为模块

gradle配置

gradle.properties里配置组件化开关

#isNeedHomeModule=true
isNeedHomeModule=false
#isNeedChatModule=true
isNeedChatModule=false
#isNeedFindModule=true
isNeedFindModule=false
#isNeedMineModule=true
isNeedMineModule=false

app的gradle里配置依赖

dependencies {
   if (isNeedHomeModule.toBoolean()) {
        implementation project(':home')
    }
    if (isNeedFindModule.toBoolean()) {
        implementation project(':find')
    }
    if (isNeedChatModule.toBoolean()) {
        implementation project(':chat')
    }
    if (isNeedMineModule.toBoolean()) {
        implementation project(':mine')
    }
    implementation project(':commonlib')
}

chat模块目录
if (!isNeedChatModule.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
defaultConfig {
        if (!isNeedChatModule.toBoolean()) {
            applicationId "tsou.cn.module_chat"
        }
        sourceSets {
            main {
                if (!isNeedChatModule.toBoolean()) {
                    manifest.srcFile  'src/main/manifest/AndroidManifest.xml'
                } else {
                    manifest.srcFile  'src/main/AndroidManifest.xml'
                }
       }
}

区分清单文件,别忘了在注册application同时初始化ARouter

跳转

传值

ARouter.getInstance().build("/chat/main")
                        .withLong("key1", 123)
                        .withString("key3", "456")
                        .navigation();

接收,Route注解注册路径,Autowired注解传递参数

@Route(path = "/chat/main")
public class MainActivity extends AppCompatActivity {

    private TextView text;

    @Autowired
    public String key3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ARouter.getInstance().inject(this);
        Toast.makeText(this, "收到传送过来的数据:" + key3, Toast.LENGTH_LONG).show();
        text = findViewById(R.id.text);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
上一篇下一篇

猜你喜欢

热点阅读