一、配置:
(1)在各个module的build.gradle文件中添加依赖:
//Arouter
api 'com.alibaba:arouter-api:1.3.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
(2)在各个module的build.gradle文件的defaultConfig{ }中添加如下代码:
//Arouter
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
二、使用:
1.在Application里初始化:
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.isDebug) {
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
}
2.页面跳转和传值:
(1)为要跳转的目标Activity添加注解:
@Route(path = "/cloud/CloudHomeActivity")
public class CloudHomeActivity extends AppCompatActivity {
(2)跳转到目标Activity并传值:
ARouter.getInstance()
.build("/cloud/CloudHomeActivity")
.withString("name", "libai")
.withInt("age", 23)
.withParcelable("person",new Person("dufu",12))
.navigation();
(3)CloudHomeActivity接收传递的参数:
@Route(path = "/cloud/CloudHomeActivity")
public class CloudHomeActivity extends AppCompatActivity {
@Autowired
public String name;
@Autowired
public int age;
@Autowired
public Person person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choud_home);
ARouter.getInstance().inject(this);
TextView textView1 = findViewById(R.id.textview1);
TextView textView2 = findViewById(R.id.textview2);
textView1.setText(name + "-" + age);
textView2.setText(person.name + "-" + person.age);
}
}
注意,这里使用ARouter协助解析参数类型时,name、age、person必须声明为public的,且名字与传参时的名字要一致,否则会报错。
也可以使用原始的方式获取参数:
name = getIntent().getStringExtra("name");
age = getIntent().getIntExtra("age",0);
person = getIntent().getParcelableExtra("person");
3.NavigationCallback监听路由结果
public class MyNavigationCallBack implements NavigationCallback {
private static final String TAG = "MyNavigationCallBack";
@Override//路由目标被发现时调用
public void onFound(Postcard postcard) {
Log.d(TAG, "onFound: 找到目标:" + postcard.getPath());
}
@Override//路由被丢失时调用
public void onLost(Postcard postcard) {
Log.d(TAG, "onLost: 没找到目标:" + postcard.getPath());
}
@Override//路由到达之后调用
public void onArrival(Postcard postcard) {
Log.d(TAG, "onArrival: 到达目标:"+ postcard.getPath());
}
@Override//路由被拦截时调用
public void onInterrupt(Postcard postcard) {
Log.d(TAG, postcard.getPath() + " 被拦截了");
}
}
在页面跳转的时候加上路由监听(mNavigationCallBack为MyNavigationCallBack的实例对象):
ARouter.getInstance()
.build(RouterPath.PAY_HOME_ACTIVIY)
.navigation(mContext,mNavigationCallBack);
4.拦截器IInterceptor
使用ARouter.getInstance().build(path).navigation(),不管是跳往哪个Activity,只要没设置绿色通道greenChannel(),都会被拦截器拦截,下面是一个拦截没登录状态、目标页的extras=-100的拦截器:
// onContinue()和onInterrupt()至少需要调用其中一种,否则不会继续路由
@Interceptor(priority = 8) //优先级越大,越先执行
public class CheckLoginIntercepter implements IInterceptor {
private static final String TAG = "CheckLoginIntercepter";
@Override//第一个参数postcard,里面包含了要跳转的目标activity的路径和要传递的参数
public void process(Postcard postcard, InterceptorCallback callback) {
if (AppApplication.getInstance().isLogined()){ //登陆了就放行
if (postcard.getExtra() == -100){ //目标页面上@Router里声明extras = -100的页面被拦截
callback.onInterrupt(null);
ARouter.getInstance()
.build(RouterPath.SETTING_ACTIVIY)
.greenChannel()
.navigation();
}else{
callback.onContinue(postcard);//放行
}
}else{ //未登陆就拦截
callback.onInterrupt(null);
ARouter.getInstance()
.build(RouterPath.LOGIN_ACTIVIY)
.greenChannel() //绿色通道,使不经过任何的拦截器
.navigation();
}
}
@Override //只在ARouter初始化的时候执行一次
public void init(Context context) {
Log.d(TAG, "init: CheckLoginIntercepter初始化了");
}
}
其中,extras 是在目标页的Router声明的,如:
@Route(path = RouterPath.PAY_HOME_ACTIVIY,extras = -100)
public class PayHomeActivity extends BaseActivity {
如果为登陆状态,要跳往PayHomeActivity,又没设置绿色通道的话,就会被拦截器拦截,从而跳到路径为RouterPath.SETTING_ACTIVIY的页面。
5.转场动画
ARouter.getInstance()
.build(RouterPath.SETTING_ACTIVIY)
.withTransition(R.anim.slide_in_right, R.anim.slide_out_left)
.navigation();