android - 在 Android 导航组件中处理 onBackPressed
问题描述
我已经在 Android 中实现了带有导航组件的导航抽屉。HomeFragment
当我单击后按时,我有 5 个片段要返回到我的片段。目前他们停留在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>
这是我onBackPressed
的 MainActivity(也是唯一一个):
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
我想结束应用程序但我不能。
解决方案
如果我的理解是正确的,那么无论您在导航流程中的哪个位置,您都希望返回 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;
});
推荐阅读
- ruby-on-rails - 是否可以使用片段缓存指定缓存或确定特定调用的缓存命中/未命中率
- javascript - 添加到 Promise 的成功和拒绝处理程序不会激活
- python - 我怎样才能提高这种图像的 tesseract 的准确性?
- javascript - 如何使用 MVC 在 IIS 中发布后修复 DataTables 不起作用
- bash - 使用 bash for 循环遍历文件
- mysql - MYSQL从文本文件输入加载空值
- php - 如果 1.1.2.254 存在于我的数据库中!使用下一个 1.1.2.253 问题
- azure - MLFlow 模型注册表 ENDPOINT_NOT_FOUND:未找到错误的 API
- android - 房间数据库关闭后如何重新打开
- javascript - 在 vue-app 中将代码从一个组件转移到另一个组件?