简体   繁体   English

片段导航和菜单。 如何删除最新片段? 检查说明

[英]Fragment navigation and menu. How to remove latest fragment? Check description

I was incapable of finding an answer for the following context:我无法为以下上下文找到答案:

Let's say we have a menu and a nav graph instanced in the main activity.假设我们在主活动中有一个菜单和一个导航图实例。 We have 3 fragment: Home, ItemList and Settings.我们有 3 个片段:Home、ItemList 和 Settings。 We add another fragment called HourPreference.我们添加另一个名为 HourPreference 的片段。 Take the next flow: I press on the bottom menu on the Settings item which takes me on the SettingsFragment.采取下一个流程:我按下“设置”项上的底部菜单,这会将我带到 SettingsFragment。 Then we press a button to take us to the HourPreferenceFragment using findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference()) .然后我们按下一个按钮,使用findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference())将我们带到 HourPreferenceFragment。 We are on HourPreferenceFragment.我们在 HourPreferenceFragment 上。 IF we press back then it takes us to SettingsFragment.如果我们按回键,它会将我们带到 SettingsFragment。 But IF we press on the home item from the bottom menu, then it takes us on that Fragment and when we press on the bottom menu on the Settings item then it directions us to the HourPreferenceFragment and not the SettingsFragment.但是,如果我们从底部菜单中按下主页项目,那么它将带我们进入该片段,当我们按下设置项目上的底部菜单时,它会将我们定向到 HourPreferenceFragment 而不是 SettingsFragment。 Code snippets down below.下面的代码片段。 How can we get to the SettingsFragment when we go along with this flow?当我们 go 随着这个流程,我们如何才能到达 SettingsFragment? I appreciate any answer whether you provide me a link to another post, I was just unable to find anything after a couple hours无论您是否向我提供指向另一篇文章的链接,我都非常感谢您的回答,几个小时后我找不到任何东西

Briefly:简要地:

The behaviour is: Press settings icon (display SettingsFragment) -> Press hour preference (display HourPreferenceFragment)-> press home icon (display HomeFragment) -> press settings icon and it takes me to HourPreference Fragment BUT I want it to take me to the SettingsFragment!行为是:按设置图标(显示 SettingsFragment)-> 按小时首选项(显示 HourPreferenceFragment)-> 按主页图标(显示 HomeFragment)-> 按设置图标,它带我到 HourPreference 片段,但我希望它带我到设置片段!

(MainActivity.onCreate): (MainActivity.onCreate):

    val navView: BottomNavigationView = binding.navView

    navController = findNavController(R.id.nav_host_fragment_activity_main)
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home,
            R.id.navigation_items_list,
            R.id.navigation_settings
        )
    )

    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

//also //还

    override fun onSupportNavigateUp(): Boolean {
          return navController.navigateUp() || super.onSupportNavigateUp()
}

(activity_main.xml) (activity_main.xml)

 <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="@color/grey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<fragment
    android:id="@+id/nav_host_fragment_activity_main"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="64dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

Also in the mobile_navigation.xml graph:同样在 mobile_navigation.xml 图中:

    <fragment
    android:id="@+id/navigation_settings"
    android:name="com.example.xpiry1.ui.settings.SettingsFragment"
    android:label="@string/title_settings"
    tools:layout="@layout/fragment_settings" >

    <action
                android:id="@+id/action_navigation_notifications_to_navigation_hour_preference"
        app:destination="@id/navigation_hour_preference"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:popUpTo="@id/navigation_settings" />
    </fragment>

    <fragment
    android:id="@+id/navigation_hour_preference"
    android:name="com.example.xpiry1.ui.settings.preferences.HourPreferenceFragment"
    android:label="Hour Preferences"
    tools:layout="@layout/fragment_hour_preference">
    </fragment>

(SettingsFragment) (设置片段)

 binding.hourPreference.root.setOnClickListener {
            findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference())

Found the core issue for this problem.找到了这个问题的核心问题。 The detailed explanation lies in this answer .详细的解释在于这个答案

For the moment, Is solved it by downgrading the the fragment dependency from 'androidx.navigation:navigation-fragment-ktx:2.4.1' to 'androidx.navigation:navigation-fragment-ktx:2.3.5' and now everything works fine.目前,通过将片段依赖项从“androidx.navigation:navigation-fragment-ktx:2.4.1”降级为“androidx.navigation:navigation-fragment-ktx:2.3.5”来解决它,现在一切正常.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM