Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

项目从 ARouter 迁移 TheRouter 时通过 Bundle 传递的参数不能正确获取 value #29

Closed
yorkshenpolestar opened this issue Nov 1, 2022 · 3 comments
Labels
duplicate This issue or pull request already exists 常见问题 常见问题

Comments

@yorkshenpolestar
Copy link

yorkshenpolestar commented Nov 1, 2022

项目从 ARouter 迁移 TheRouter 时通过 Bundle 传递的参数不能正确获取 value

Example code

NavigatorTestActivity

Bundle bundle = new Bundle();
bundle.putInt("test_int_value", 1);

TheRouter.build(HomePathIndex.HOME)
    .with(bundle)
    .navigation();

NavigatorTargetActivity

Bundle bundle = getIntent().getExtras();
int intValueFromBundle = bundle.getInt("test_int_value", 0);

final TextView textView1 = findViewById(R.id.textview1);
textView1.setText("接收int值传递:integer2=" + intValueFromBundle);

Expected Result

integer2=1

Bundle 结构是下面这种

image

Actual Result:

integer2=0

image

Question

看起来与 ARouter 传递 Bundle 之后获取 Bundle 参数的方式不一致,不是能很平滑的迁移到 TheRouter.

之前旧项目使用 ARouter 时都是使用的

 ARouter.getInstance().build(path).with(bundle)
                .navigation(activity, requestCode)

传递参数,然后在对应页面使用 intent.extras?.getBoolean(KEY_WEB_VIEW_CLOSE_MODE, false) 这种方式获取参数,如果迁移到 TheRouter 之后必须使用 @Autowired 方式获取参数改动工作量会比较大。

我看了下源码:

fun with(value: Bundle?): Navigator = withBundle(KEY_BUNDLE, value)

我可以通过下面的方式获取传递的值

Bundle bundle = getIntent().getBundleExtra("therouter_bundle");
int intValueFromBundle = bundle.getInt("test_int_value", 0);

但是写法怪怪的,和以前用 ARouter 的写法也不兼容,是我写法有问题吗?或者有什么好的能够平滑迁移的建议?

@kymjs
Copy link
Collaborator

kymjs commented Nov 2, 2022

TheRouter.build("")
	.fillParams { bundle ->
	    bundle.putAll(yourBundle)
	}.navigation()

参考迁移文档 https://therouter.cn/docs/2022/09/05/01

@yorkshenpolestar
Copy link
Author

谢谢

@yorkshenpolestar
Copy link
Author

image

一个简单的解决方法,遇到的可以参考一下

@kymjs kymjs added the duplicate This issue or pull request already exists label Nov 4, 2022
@kymjs kymjs added the 常见问题 常见问题 label Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists 常见问题 常见问题
Projects
None yet
Development

No branches or pull requests

2 participants