一. 依赖
android {
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
}
}
---------------------
implementation 'com.alibaba:arouter-api:1.3.1'
implementation 'com.alibaba:arouter-compiler:1.1.4'
二.在Application初始化
public class Appextends Application{
@Override
public void onCreate() {
super.onCreate();
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
ARouter.init(this);
}
}
三.配置路由路径("/XX/XX") 传值:地址最少两级 /xx/xxx
示例:
public class MainActivityextends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.tv_main).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ARouter.getInstance().build("/home/home_main")
.withString("title", "传值")
.navigation();
}
});
}
}
跳转页面:
@Route(path ="/home/home_main")
public class HomeActivityextends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//接收传值
String title = getIntent().getStringExtra("title");
Toast.makeText(this,title,Toast.LENGTH_SHORT).show();
}
}
三.
网友评论