[英]selectedItemId doesn't work anymore after androidx.navigation:navigation library update
I have the following code:我有以下代码:
activityBinding.bottomNav.selectedItemId = R.id.myFragment
that was working fine with androidx.navigation:navigation-*
version 2.3.5 but is not working with version 2.4.2 and any other 2.4.* version.这适用于
androidx.navigation:navigation-*
版本 2.3.5,但不适用于版本 2.4.2 和任何其他 2.4.* 版本。
I've searched everywhere but I haven't found a solution to make it work (also alternative code like menu.findItem(R.id.myFragment).isChecked = true
didn't work).我到处搜索,但我还没有找到使它工作的解决方案(还有替代代码,如
menu.findItem(R.id.myFragment).isChecked = true
没有工作)。
Any idea of what I'm doing wrong?知道我做错了什么吗?
It is working fine for me I am using 2.4.1
version of androidx.navigation:navigation-*
它对我来说工作正常我正在使用
androidx.navigation:navigation-*
的2.4.1
版本
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.mainFragment, R.id.recentsFragment, R.id.favouriteFragment
-> {
// do your Code
}
else -> {
//do your code
}
}
Log.i("TLogs", "onCreate: ${bottomNavigationView.selectedItemId}")
}
You can check it according to the example I have done and attached.大家可以按照我做的和附上的例子查看。
Log is showing the current item Id日志显示当前项目 ID
Since the new library, if with the navigator you set a fragment as starting fragment and you select with selectedItemId the same fragment, the result will be that you'll click on it but nothing will happen.由于新库,如果使用导航器将一个片段设置为起始片段,并且将 select 与 selectedItemId 设置为相同的片段,结果将是您将单击它但什么也不会发生。
Putting the starting fragment in the nav_graph to something else and then selecting the right one using selectedItemId
make it working.将 nav_graph 中的起始片段放入其他内容,然后使用
selectedItemId
选择正确的片段使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.