【发布时间】:2019-05-20 08:50:04
【问题描述】:
我已经在 Android 中实现了带有导航组件的导航抽屉。当我点击后按时,我有 5 个片段要返回到我的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 中时,我想结束应用程序但我不能。
【问题讨论】:
-
无法从您的陈述中理解问题所在。你能详细说明你想在 backPress 上做什么吗?我从你的陈述中总结出两种情况。一个如果您从导航中选择了任何项目,那么在您想要进入主屏幕的背景下。并再次在后台想要退出应用程序。其次,在您想要将片段更改为主页并立即退出时。
-
首先,当你在互相替换片段时,你必须使用替换片段而不是添加片段,这样它们就不会留在 backStack 中
-
你在哪里看到
replace或add方法o.O
标签: android android-fragments navigation-drawer android-architecture-navigation