ARouter是阿里巴巴开源出来的一款android路由框架,github地址为 : https://github.com/alibaba/ARouter
至于ARouter的诸多好处我就不介绍了,这里主要讲解在项目组件化下,ARouter的一些简单使用
先贴上工程目录:
工程一共分为4个模块,基础组件app、基础服务(包涵路由服务)basecommonlibrary模块、业务模块libraryone、业务模块librarytwo;
在4个模块的gradle文件当中加入如下代码:
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
}
dependencies {
...
compile 'com.alibaba:arouter-api:1.2.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
}
依赖包的具体版本可自行选择。
接下来就是具体讲解ARouter如何在这几个模块中充当路由的角色了。
首先是app模块中定义的一个页面,布局很简单,就不贴代码了,效果如下;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
RouterCommonUtil.startMainTextActivity(this, "测试文本");
break;
case R.id.bt2:
RouterCommonUtil.startMainImageActivity(this, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492584424522&di=5dcde77431914e0b944b8af9ff5f9277&imgtype=jpg&src=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D1489971249%2C1618541505%26fm%3D214%26gp%3D0.jpg");
break;
case R.id.bt3:
RouterCommonUtil.startLibraryOneActivity(this);
break;
case R.id.bt4:
RouterCommonUtil.startLibraryTwoActivity(this);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 1002) {
Toast.makeText(this, data.getStringExtra("arg2"), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.bt1) {
RouterCommonUtil.startMainActivity(this);
} else if (i == R.id.bt2) {
RouterCommonUtil.startLibraryTwoActivity(this);
}
}
import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.basecommonlibrary.router.IInterceptor.InterruptCallback;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
/**
* @author: xiewenliang
* @Filename:
* @Description:
* @date: 2017/4/25 15:06
*/
public class RouterCommonUtil {
private static void toastInterruptInfo(final Activity activity, final Postcard postcard) {
if (postcard.getTag() != null && postcard.getTag() instanceof String) {
Observable.create(new Observable.OnSubscribe() {
@Override
public void call(Subscriber super String> subscriber) {
subscriber.onNext((String) postcard.getTag());
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1() {
@Override
public void call(String s) {
if (!TextUtils.isEmpty(s) && activity != null) {
Toast.makeText(activity, (String) postcard.getTag(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
public static void startMainActivity(final Activity activity) {
ARouter.getInstance().build("/ui/主页").navigation(activity, new InterruptCallback() {
@Override
public void onInterrupt(Postcard postcard) {
toastInterruptInfo(activity, postcard);
}
});
}
public static void startMainTextActivity(final Activity activity, String value) {
ARouter.getInstance().build("/ui/text", "文本").withString("arg1", value).navigation(activity, 1001, new InterruptCallback() {
@Override
public void onInterrupt(Postcard postcard) {
toastInterruptInfo(activity, postcard);
}
});
}
public static void startMainImageActivity(final Activity activity, String value) {
ARouter.getInstance().build("/ui/image", "图片").withString("arg1", value).navigation(activity, new InterruptCallback() {
@Override
public void onInterrupt(Postcard postcard) {
toastInterruptInfo(activity, postcard);
}
});
}
public static void startLibraryOneActivity(final Activity activity) {
ARouter.getInstance().build("/libraryOne/主页").navigation(activity, new InterruptCallback() {
@Override
public void onInterrupt(Postcard postcard) {
toastInterruptInfo(activity, postcard);
}
});
}
public static void startLibraryTwoActivity(final Activity activity) {
ARouter.getInstance().build("/libraryTwo/主页").navigation(activity, new InterruptCallback() {
@Override
public void onInterrupt(Postcard postcard) {
toastInterruptInfo(activity, postcard);
}
});
}
}
@Route(path = "/libraryOne/主页", extras = CommonStation.CHECK_LOADING)
import android.content.Context;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.example.basecommonlibrary.CommonStation;
/**
* @author: xiewenliang
* @Filename:
* @Description:
* @date: 2017/4/25 15:37
*/
@Interceptor(priority = 1, name = "拦截未登录")
public class TestOneIInterceptor implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
if (postcard.getExtra() == CommonStation.CHECK_LOADING && !CommonStation.checkLoading()) {
callback.onInterrupt(new RuntimeException("账号未登录"));
} else {
callback.onContinue(postcard);
}
}
@Override
public void init(Context context) {
}
}