Android导航组件中的手柄

l3zydbqr  于 2023-03-16  发布在  Android
关注(0)|答案(3)|浏览(121)

我已经在Android中实现了带有导航组件的导航抽屉。我有5个片段,当我点击back pressed时,我想回到我的HomeFragment。目前,它们停留在BackStack上,不转到我想要的片段,而是转到第一个片段。这是我的nav_graph

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            app:startDestination="@id/setupFragment"
            android:id="@+id/treasure_nav"
            android:label="Pick a country">
    <fragment android:id="@+id/homeFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.home.HomeFragment"
              android:label="Home"
              tools:layout="@layout/fragment_home">
        <action android:id="@+id/action_home_fragment_to_namesFragment2"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/namesFragment"/>
        <action android:id="@+id/action_home_fragment_to_quranFragment"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/quranFragment"/>
        <action android:id="@+id/action_homeFragment_to_tasbeehFragment"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/tasbeehFragment"/>
        <action android:id="@+id/action_homeFragment_to_galleryFragment"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/galleryFragment"/>
        <action android:id="@+id/action_homeFragment_to_newsFragment"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/newsFragment"/>
        <action android:id="@+id/action_homeFragment_to_settingsFragment"
                app:popUpTo="@id/homeFragment"
                app:destination="@id/settingsFragment"/>
    </fragment>
    <fragment
            android:id="@+id/namesFragment"
            android:name="com.stavro_xhardha.pockettreasure.ui.names.NamesFragment"
            android:label="Names of Allah"
            tools:layout="@layout/fragment_names"/>
    <fragment
            android:id="@+id/quranFragment"
            android:name="com.stavro_xhardha.pockettreasure.ui.quran.QuranFragment"
            android:label="Quran"
            tools:layout="@layout/fragment_quran"/>
    <fragment android:id="@+id/tasbeehFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.tasbeeh.TasbeehFragment"
              android:label="Tasbeeh"
              tools:layout="@layout/fragment_tasbeeh"/>
    <fragment android:id="@+id/galleryFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.gallery.GalleryFragment"
              android:label="Gallery"
              tools:layout="@layout/fragment_gallery"/>
    <fragment android:id="@+id/newsFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.news.NewsFragment"
              android:label="News"
              tools:layout="@layout/fragment_news"/>
    <fragment android:id="@+id/settingsFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.settings.SettingsFragment"
              android:label="Settings"
              tools:layout="@layout/fragment_settings"/>
    <fragment android:id="@+id/setupFragment"
              android:name="com.stavro_xhardha.pockettreasure.ui.setup.SetupFragment"
              android:label="Pick country"
              tools:layout="@layout/fragment_setup">
        <action android:id="@+id/action_setupFragment_to_homeFragment3"
                app:destination="@+id/homeFragment"
                app:launchSingleTop="true"
                app:popUpTo="@+id/treasure_nav"
                app:popUpToInclusive="true"/>
    </fragment>
</navigation>

这是我的MainActivity中的onBackPressed(也是唯一的一个):

override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
                super.onBackPressed()
        }
    }

编辑:当我删除super.onBackPressed()并将其替换为:findNavController(R.id.nav_host_fragment).popBackStack(R.id.homeFragment, false)我实现了我想要的。唯一的问题是,当我在homeFragment中时,我想结束应用程序,但我不能。

5kgi1eie

5kgi1eie1#

如果我的理解是正确的,无论你在导航流中的什么地方,你都想回到HomeFragment。对于这种情况,你可以尝试通过addOnBackPressedCallback在你的Fragments上注册OnBackPressedCallback,然后调用popBackStack来导航到你的HomeFragment。尝试将这个添加到Fragments的onViewCreated,它需要在按下时回到HomeFragment:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        navController = Navigation.findNavController(view);

        requireActivity().addOnBackPressedCallback(getViewLifecycleOwner(), () -> {
               navController.popBackStack(R.id.homeFragment, false);
            });
            return true;
        });
7fhtutme

7fhtutme2#

如果您想要在按下HomeFragment中的上一个按钮时关闭应用程序,只需指定将您导航到此目标的最后一个操作的以下属性:

  1. app:popUpToInclusivetrue
    1.将您导航到此处的最后一个片段(SetupFragment)(HomeFragment)的app:popUpTo
    这意味着像这样修改代码:
<fragment android:id="@+id/setupFragment"
          android:name="com.stavro_xhardha.pockettreasure.ui.setup.SetupFragment"
          android:label="Pick country"
          tools:layout="@layout/fragment_setup">
    <action android:id="@+id/action_setupFragment_to_homeFragment3"
            app:destination="@+id/homeFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/setupFragment"          // this line changes
            app:popUpToInclusive="true" />            // and this requires too
</fragment>
d4so4syb

d4so4syb3#

我创建这个扩展函数就是为了实现这个目的:

fun Fragment.setupOnBackPressedCallback(block: () -> Unit) {
    requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner,
        object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() = block.invoke()
        }
    )
}

在你的片段中,你所要做的就是调用它来传递一个lambda:

setupOnBackPressedCallback {
    // Do what you want when you press the back button
}

相关问题