路径配置都是一样,以下只介绍跳转方式
-
使用Uri应用内跳转
EventBusBean eventBusBean = new EventBusBean(); eventBusBean.setProject("android"); eventBusBean.setNum(3); Uri testUriMix = Uri.parse("arouter://tsou.cn.huangxiaoguo/me/main/EventBus"); ARouter.getInstance().build(testUriMix) .withString("name", "haungxiaoguo") .withLong("age", 25) .withParcelable("eventbus", eventBusBean) .navigation();
- 旧版本转场动画
//旧版本转场动画
ARouter.getInstance()
.build(RouteUtils.Me_Test)
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
.navigation(getContext());//context上下文不传无效果
- 新版本转场动画
//新版本转场动画
if (Build.VERSION.SDK_INT >= 16) {
ActivityOptionsCompat compat = ActivityOptionsCompat.
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
ARouter.getInstance()
.build(RouteUtils.Me_Test)
.withOptionsCompat(compat)
.navigation();
} else {
UIUtils.showToast("API < 16,不支持新版本动画");
}
- 通过URL跳转(webview)
//通过URL跳转(webview)
ARouter.getInstance()
.build(RouteUtils.Me_WebView)
.withString("url", "file:///android_asset/schame-test.html")
.navigation();
拦截器操作
首先实现IInterceptor:
package tsou.cn.lib_base.interceptor;
import android.content.Context;
import android.util.Log;
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.alibaba.android.arouter.launcher.ARouter;
import tsou.cn.lib_base.utils.RouteUtils;
/**
* Created by Administrator on 2017/12/6 0006.
* 添加拦截器的时候,建议clean再打包运行,不然会出现,无效的情况
* <p>
* 切记一个项目里面priority的值保证唯一,不然会出毛病
*/
@Interceptor(priority = 1, name = "重新分组进行拦截")
public class MyDataInterceptor implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
if (postcard.getGroup().equals("needLogin")) {
Log.e("huangxiaoguo", "需要去登陆");
//直接执行
// callback.onContinue(postcard);
//直接拦截,走onLost方法
// callback.onInterrupt(null);
//添加数据
// postcard.withString("extra", "我是在拦截器中附加的参数");
// callback.onContinue(postcard);
callback.onInterrupt(null);
ARouter.getInstance().build(RouteUtils.Me_Login)
.withString("path", postcard.getPath()).navigation();
} else {
postcard.withString("extra", "我是在拦截器中附加的参数");
callback.onContinue(postcard);
}
}
@Override
public void init(Context context) {
}
}
接收未拦截时拦截器添加的数据(postcard.withString(“extra”, “我是在拦截器中附加的参数”);)
private void initData() {
extra = getIntent().getStringExtra("extra");
}
-
拦截器操作(利用重新分组拦截)
/** * 如果利用重新分组,就需要在build中进行指定的分组不然没有效果 */ ARouter.getInstance() .build(RouteUtils.Me_Test2, "needLogin") .navigation(getContext(), new NavCallback() { @Override public void onFound(Postcard postcard) { super.onFound(postcard); //路由目标被发现时调用 Log.e("huangxiaoguo", "发现了"); } @Override public void onLost(Postcard postcard) { super.onLost(postcard); //路由被丢失时调用 Log.e("huangxiaoguo", "丢失了"); } @Override public void onArrival(Postcard postcard) { //路由到达之后调用 Log.e("huangxiaoguo", "到达了"); } @Override public void onInterrupt(Postcard postcard) { super.onInterrupt(postcard); //路由被拦截时调用 Log.e("huangxiaoguo", "拦截了"); } });
- 拦截器操作(利用原有分组)
/** * 专门的分组,这里就叫做needLogin组,凡是在这个组下的,都会进行登录操作 */ public static final String NeedLogin_Test3 = "/needLogin/main/Test3";
@Route(path = RouteUtils.NeedLogin_Test3)
public class Test3Activity extends AppCompatActivity {
//拦截器操作(利用原有分组)
ARouter.getInstance().build(RouteUtils.NeedLogin_Test3).navigation();
- 拦截器操作(绿色通道,跳过拦截器)
//拦截器操作(绿色通道,跳过拦截器)
ARouter.getInstance().build(RouteUtils.NeedLogin_Test3)
.withString("extra", "我是绿色通道直接过来的,不经过拦截器")
.greenChannel()
.navigation();
- 依赖注入
必须先初始化JsonServiceImpl实现SerializationService
package tsou.cn.lib_base.JsonService;
import android.content.Context;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.service.SerializationService;
import com.alibaba.fastjson.JSON;
import java.lang.reflect.Type;
import tsou.cn.lib_base.utils.RouteUtils;
/**
* 依赖注入使用,注意:必须实现SerializationService进行注册,
*/
@Route(path = RouteUtils.Home_Json_Service,name = "序列化JavaBean使用")
public class JsonServiceImpl implements SerializationService {
@Override
public void init(Context context) {
}
@Override
public <T> T json2Object(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
@Override
public String object2Json(Object instance) {
return JSON.toJSONString(instance);
}
@Override
public <T> T parseObject(String input, Type clazz) {
return JSON.parseObject(input, clazz);
}
}
实现跳转
/**
* 序列化过得
* 必须先初始化JsonServiceImpl实现SerializationService
*/
EventBusBean eventBusBean = new EventBusBean();
eventBusBean.setProject("android");
eventBusBean.setNum(3);
/**
* 普通的javaBean
*/
JavaBean javaBean = new JavaBean();
javaBean.setName("huangxiaoguo");
javaBean.setAge(25);
List<JavaBean> objList = new ArrayList<>();
objList.add(javaBean);
Map<String, List<JavaBean>> map = new HashMap<>();
map.put("testMap", objList);
ARouter.getInstance().build(RouteUtils.Me_Inject)
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://www.baidu.com")
.withParcelable("pac", eventBusBean)
.withObject("obj", javaBean)
.withObject("objList", objList)
.withObject("map", map)
.navigation();
接收数据
package tsou.cn.module_me.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import java.util.List;
import java.util.Map;
import tsou.cn.lib_base.bean.EventBusBean;
import tsou.cn.lib_base.bean.JavaBean;
import tsou.cn.lib_base.utils.RouteUtils;
import tsou.cn.module_me.R;
/**
* 依赖注入使用,注意:必须实现SerializationService进行注册,
*/
@Route(path = RouteUtils.Me_Inject)
public class InjectActivity extends AppCompatActivity {
@Autowired
String name = "hahahha";
@Autowired
int age = 13;
@Autowired(name = "boy")//映射参数名
boolean sex;
@Autowired
long high = 160;
@Autowired
String url;
@Autowired
EventBusBean pac;
@Autowired
JavaBean obj;
@Autowired
List<JavaBean> objList;
@Autowired
Map<String, List<JavaBean>> map;
@Autowired
int height = 21;//上页面没有传递
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inject);
ARouter.getInstance().inject(this);
initView();
}
private void initView() {
mTextView = (TextView) findViewById(R.id.textView);
String params = String.format(
"name=%s,\n age=%s, \n height=%s,\n girl=%s,\n high=%s,\n url=%s,\n pac=%s,\n obj=%s \n" +
" objList=%s, \n map=%s",
name,
age,
height,
sex,
high,
url,
pac.getProject(),
obj.getName(),
objList.get(0).getName(),
map.get("testMap").get(0).getName()
);
mTextView.setText(params);
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦