kotlin 在导航栏中的片段之间导航时出现问题

xuo3flqw  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(183)

我在导航栏中的片段之间导航时遇到了问题。这是我的情况:我有3个主要片段:主页列表设置****主页有2个子片段:添加和编辑所以,如果我用导航栏在3个主要片段之间导航一切都好,那么如果我进入2个“子片段”中的一个,然后,(而不是“导航向上”之前,然后改变主要)我直接选择其他2个“主要”之一,我会去那里,但是如果我回到列表,我会看到子片段。我必须向上导航才能再次看到列表我想要的是如果我在其中一个“子”中切换主片段(List的儿子),然后我再次选择List,我想看到List,而不是我离开的sub。
我的代码:
导览

  1. <fragment
  2. android:id="@+id/homeFragment"
  3. android:name="com.somi.fidelitycardconnect.ui.home.HomeFragment"
  4. android:label="Home"
  5. tools:layout="@layout/fragment_home" />
  6. <fragment
  7. android:id="@+id/settingsFragment"
  8. android:name="com.somi.fidelitycardconnect.ui.settings.SettingsFragment"
  9. android:label="Impostazioni"
  10. tools:layout="@layout/fragment_settings" />
  11. <fragment
  12. android:id="@+id/listFragment"
  13. android:name="com.somi.fidelitycardconnect.ui.list.ListFragment"
  14. android:label="Lista clienti"
  15. tools:layout="@layout/fragment_list">
  16. <action
  17. android:id="@+id/action_listFragment_to_addFormFragment"
  18. app:destination="@id/addFormFragment" />
  19. <action
  20. android:id="@+id/action_listFragment_to_editFormFragment"
  21. app:destination="@id/editFormFragment" />
  22. </fragment>
  23. <fragment
  24. android:id="@+id/addFormFragment"
  25. android:name="com.somi.fidelitycardconnect.ui.form.AddFormFragment"
  26. android:label="Aggiungi cliente"
  27. tools:layout="@layout/fragment_add_form">
  28. </fragment>
  29. <fragment
  30. android:id="@+id/editFormFragment"
  31. android:name="com.somi.fidelitycardconnect.ui.form.EditFormFragment"
  32. android:label="Informazioni cliente"
  33. tools:layout="@layout/fragment_edit_form">
  34. </fragment>

主要活动:

  1. binding = ActivityMainBinding.inflate(layoutInflater)
  2. setContentView(binding.root)
  3. val navView: BottomNavigationView = binding.navView
  4. val navController = findNavController(R.id.nav_host_fragment_activity_main)
  5. // Passing each menu ID as a set of Ids because each
  6. // menu should be considered as top level destinations.
  7. appBarConfiguration = AppBarConfiguration(
  8. setOf(
  9. R.id.homeFragment,
  10. R.id.listFragment,
  11. R.id.settingsFragment
  12. )
  13. )
  14. setupActionBarWithNavController(navController, appBarConfiguration)
  15. navView.setupWithNavController(navController)
  16. }
  17. override fun onSupportNavigateUp(): Boolean {
  18. val navController = findNavController(R.id.nav_host_fragment_activity_main)
  19. return navController.navigateUp(appBarConfiguration)
  20. || super.onSupportNavigateUp()
  21. }
  22. }

enter image description hereenter image description here显示器

我想要的是如果我切换主片段时,我的一个“子”(儿子的列表),然后再次我选择列表,我想看到列表,而不是子,我离开。

uqxowvwt

uqxowvwt1#

如果你想按下bottomNav项中的一个并从默认位置开始,你需要弹出其他目标。使用下面的代码来实现它:

  1. navView.setupWithNavController(navController)
  2. navView.setOnItemSelectedListener { item ->
  3. NavigationUI.onNavDestinationSelected(item, navController)
  4. navController.popBackStack(item.itemId, inclusive = false)
  5. true
  6. }

相关问题