I am using Google's recommended single activity pattern with Android Jetpack's Navigation component.
fragment 1 是验证屏幕。用户通过身份验证并导航到 Fragment 2 后,我想按下 Android 后退按钮关闭应用程序,并删除应用程序栏中的后退箭头。
我找到了方法,例如
onBackPressedDispatcher
, 从后按中添加/删除功能,但不会删除后箭头。我也试过
app:popUpTo="@+id/firstFragment"
从 Fragment 1 导航到 Fragment 2 时,但这也不起作用。这应该可以用一行代码来指定。还在努力寻找。有小费吗?
最佳答案
您需要删除 fragment1
来自 back-stack
当导航到 fragment2
fragment 1
<fragment
android:id="@+id/fragment1"
android:name="packagenameforFragment1"
android:label="fragment1"
tools:layout="@layout/fragment_1" >
<action
android:id="@+id/action_Fragment1_to_Fragment2"
app:destination="@id/Fragment2_id"
app:launchSingleTop="true"
app:popUpTo="@+id/your_MainGraph_id"
app:popUpToInclusive="true" />
然后当你从 fragment1 导航到 fragment2 时调用这个
findNavController(fragment).navigate(R.id.action_Fragment1_to_Fragment2)
要从 Fragment2 中删除后退按钮,您可以使用它
在 Activity onCreate()
val appBarConfiguration = AppBarConfiguration
.Builder(R.id.your_fragment2_id,R.id.any_other_ids_you_want)
.build()
然后像这样设置你的工具栏
setupActionBarWithNavController(this, yourNavController, appBarConfiguration)
关于android - 如何使用 Android Jetpack 的导航组件禁用后退导航并删除 fragment 上的后退箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425710/