用导航组件从背面删除片段
我有一个带有 navHost 片段的 PinCreateActivity 和 2 个片段 PinSetup 和 PinCreate 片段。
当 Activity 启动时,PinSetup 是默认片段,之后通过单击按钮导航到 PinCreate 片段。我想要的是来自 PinCreate 片段,当用户单击后退按钮时,不要像 PinCreateActivity 那样转到 PinSetup 并导航到后退堆栈。所以我想当我从 PinSetup 导航到 PinCreate 片段时,我必须从 backStack 中删除 PinSetup。我怎样才能做到这一点?
navigation_graph.xml
<?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"
android:id="@+id/pin_create_nav_graph"
app:startDestination="@id/pinSetupFragment">
<fragment
android:id="@+id/pinSetupFragment"
android:name="com.example.ui.fragments.pin.PinSetupFragment"
android:label="Create PIN"
tools:layout="@layout/fragment_pin_setup" >
<action
android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
app:destination="@id/pinCreateFragment" />
</fragment>
<fragment
android:id="@+id/pinCreateFragment"
android:name="com.example.ui.fragments.pin.PinCreateFragment"
android:label="Your PIN"
tools:layout="@layout/fragment_pin_create" />
</navigation>
PinCreateActivity
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
...
navController = Navigation.findNavController(this, R.id.pin_create_host_fragment)
// onClick
navController.navigate(R.id.action_pinSetupFragment_to_pinCreateFragment)
}
I have a PinCreateActivity with a navHost fragment with 2 Fragments PinSetup and PinCreate fragments.
When the Activity launches, the PinSetup is the default fragment and after that with a click button i navigate to the PinCreate fragment. What i want is from the PinCreate fragment, when the User clicks the back button NOT to go to the PinSetup and navigate to the backstack as would the PinCreateActivity would do. So i guess when i navigate from the PinSetup to the PinCreate fragment i have to remove the PinSetup from the backStack. How can i do that?
navigation_graph.xml
<?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"
android:id="@+id/pin_create_nav_graph"
app:startDestination="@id/pinSetupFragment">
<fragment
android:id="@+id/pinSetupFragment"
android:name="com.example.ui.fragments.pin.PinSetupFragment"
android:label="Create PIN"
tools:layout="@layout/fragment_pin_setup" >
<action
android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
app:destination="@id/pinCreateFragment" />
</fragment>
<fragment
android:id="@+id/pinCreateFragment"
android:name="com.example.ui.fragments.pin.PinCreateFragment"
android:label="Your PIN"
tools:layout="@layout/fragment_pin_create" />
</navigation>
PinCreateActivity
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
...
navController = Navigation.findNavController(this, R.id.pin_create_host_fragment)
// onClick
navController.navigate(R.id.action_pinSetupFragment_to_pinCreateFragment)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在弹出时需要一些额外的逻辑,则可以通过Kotlin代码进行编程执行操作。
但是,如果您只需要从背堆中删除
pinsetupfragment
,就可以在导航图XML文件上执行此操作。因此,如果您只需弹出片段而没有任何其他额外的逻辑,那么从背堆中弹出
pinsetupfragment
的最佳方法将更新您的navigation_graph.graph.xml
文件。只需将这两行添加到您的操作中:
因此,您的
navigation_graph.xml
文件将是这样的:You can do that programmatically from Kotlin code if you need some extra logic while doing pop up.
But if you just need to remove
PinSetupFragment
from your back stack, you can do that on your navigation graph xml file.So, if you are just going to pop up your fragment without any other extra logic, best way to pop up
PinSetupFragment
from your back stack would be updating yournavigation_graph.xml
file.Just add these two lines to your action:
As a result, your
navigation_graph.xml
file will be like this: