片段导航和菜单。如何删除最新的片段?检查描述

发布于 2025-01-18 07:42:02 字数 3589 浏览 0 评论 0原文

我无法找到以下上下文的答案:

假设我们在主要活动中有一个菜单和一个导航图实例。我们有 3 个片段:Home、ItemList 和 Settings。我们添加另一个名为 HourPreference 的片段。 采取下一个流程:我按“设置”项上的底部菜单,这会将我带到“SettingsFragment”。然后,我们按下一个按钮,使用 findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference()) 将我们带到 HourPreferenceFragment。我们在 HourPreferenceFragment。如果我们按回键,它将带我们进入 SettingsFragment。但是,如果我们从底部菜单中按主页项目,那么它会将我们带到该 Fragment,并且当我们按“设置”项目上的底部菜单时,它会将我们引导至 HourPreferenceFragment 而不是 SettingsFragment。代码片段如下。当我们按照这个流程进行时,如何才能到达SettingsFragment?无论您是否向我提供另一篇文章的链接,我都非常感谢您的回答,几个小时后我只是找不到任何内容。

简而言之:

行为是:按设置图标(显示 SettingsFragment)->按小时偏好(显示HourPreferenceFragment)->按主页图标(显示 HomeFragment)->按设置图标,它会将我带到 HourPreference Fragment,但我希望它将我带到 SettingsFragment!

(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)

//还有

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

(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" />

同样在 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())

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. We add another fragment called HourPreference.
Take the next flow: I press on the bottom menu on the Settings item which takes me on the SettingsFragment. Then we press a button to take us to the HourPreferenceFragment using findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference()). We are on HourPreferenceFragment. IF we press back then it takes us to 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. Code snippets down below. How can we get to the SettingsFragment when we go along with this flow? 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!

(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)

 <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:

    <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())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2025-01-25 07:42:02

找到了这个问题的核心问题。详细说明在于此答案

目前,通过从'androidx.Navigation:Navigation-Fragment-ktx:2.4.1'降低片段依赖性来解决它。

'androidx.Navigation:Navigation-Fragment-ktx:2.3.5'
现在一切都很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文