ARouter传值

ARouter是一个用于帮助Android App进行组件化改造的框架 —— 支持模块间的路由、通信、解耦。ARouter的典型应用场景有:

  1. 从外部URL映射到内部页面,以及参数传递与解析;
  2. 跨模块页面跳转,模块间解耦;
  3. 拦截跳转过程,处理登陆、埋点等逻辑;
  4. 跨模块API调用,通过控制反转来做组件解耦。

用法之一就是传递:

“组件化基础ARouter”举个例子:在不同的LoginActivty中有一个Button,点击Button后打开模块HomeActivity

在不使用Arouter的情况下,更简单的方法就是intent传递

现在来介绍一下Arouter传递的方法

1:通过build 来传递不同的模块 通过with来传递对应的类型参数:

ARouter.getInstance()
.build(AroutePath.HOME)
.withInt("intValue", 10)
.withString("strValue", "hello")
.withParcelable("user", user)
.navigation();

2:之间需要加入ARouterPath 用来找到传递的位置

public class ARouterPath {
    public static final String HOME = "/app/home";
    
}

3:接收Arouter传递的数据需要加入@Autowired注解

@Route(path = ArouterPath.Main)
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    // 2.需要由ARouter传递的参数需要加@Autowired注解
    @Autowired
    public int intValue;
    @Autowired
    public String strValue;
    @Autowired
    public User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
       
        firstIntTextView.setText("intValue:"+intValue);
        firstStrTextView.setText("strValue"+strValue);
        firstUserTextView.setText("User:"+user.name+":"+user.age);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值