无法使用导航组件清除所有片段的后堆栈 [英] Unable to clear the back stack of all fragments with Navigation Components
问题描述
我正在使用 Android 导航组件并面临一个奇怪的问题.无论我尝试什么,我都无法清除向上按钮的返回堆栈.
I am using the Android Navigation Components and am facing one weird issue. I am not able to clear the back stack for the up button, no matter what I tried.
我的导航图如下所示:
我们以 LoginFragment 为例,
Let's take the LoginFragment for example,
<fragment
android:id="@+id/loginFragment"
android:name="com.yashovardhan99.firebaselogin.LoginFragment"
android:label="Login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_loginFragment_to_welcomeFragment"
app:destination="@id/welcomeFragment"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true" />
</fragment>
要导航的 Java 代码是:
The Java code to navigate is:
navController.navigate(
LoginFragmentDirections.actionLoginFragmentToWelcomeFragment());
我已将 PopUpTo 设置为图形 ID,并将 Inclusive 设置为 true.当移动到 WelcomeFragment 时,这应该清除返回堆栈.但是,WelcomeFragment 仍然在操作栏上显示向上按钮,按下它会将我带回 PreLoginFragment(这是图表的主目的地).奇怪的是,按后退按钮会让我按预期退出应用程序.
I have set the PopUpTo to the graph ID and set Inclusive to true. This should clear the back stack when moving to WelcomeFragment. However, the WelcomeFragment still shows the Up button on the action bar and pressing it takes me back to the PreLoginFragment (which is the home destination for the graph). Strangely, pressing the back button takes me out of the app as expected.
推荐答案
我用 AppBarConfiguration
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.preLoginFragment,
R.id.welcomeFragment
)
.build()
然后,您需要调用 setupActionBarWithNavController(this, navController, appBarConfiguration)
这里我声明了两个顶级片段,其中后退箭头不会显示在应用栏中.
Here I am declaring two top level fragments where the back arrow won't be shown in the app bar.
这篇关于无法使用导航组件清除所有片段的后堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!