简体   繁体   English

selectedItemId 在 androidx.navigation:navigation 库更新后不再工作

[英]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.

相关问题 androidx.navigation 库更新到 2.2.2 版本发布构建问题 - androidx.navigation library update to 2.2.2 version Release build issues Androidx 导航视图 - `setNavigationItemSelectedListener` 不起作用 - Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work 如何制作 RTL appBarConfiguration androidx.navigation? - How to make RTL appBarConfiguration androidx.navigation? androidx.navigation 菜单项的多个目的地 - androidx.navigation multiple destinations for menu item 使用 androidx.navigation 时随机崩溃 - Random crashes when using androidx.navigation 找不到 androidx.navigation:navigation-compose: - Could not find androidx.navigation:navigation-compose: 警告:NavController.setNavigatorProvider 只能从同一个库组中调用 (groupId=androidx.navigation) - Warning: NavController.setNavigatorProvider can only be called from within the same library group (groupId=androidx.navigation) 为什么androidx.navigation 使用fragment 的relace? - why androidx.navigation use fragment's relace? 如何使用androidx.navigation从片段返回数据? - How to return data from a fragment using androidx.navigation? 使用 androidx.navigation 时 Activity 重置工具栏标题 - toolbar title reset by Activity when using androidx.navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM