假设我有片段 a->b->c ,但“a”是启动画面,所以我希望“b”成为堆栈中的第一个片段并永远抛出“a”,所以当我是“b”时然后按“后退”系统按钮 - 我关闭应用程序
在SupportFragmentManager中,我使用了replace()并且有效。但我在这里怎样才能实现呢?
这是一个例子
图.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"
app:startDestination="@id/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="com.example.***.fragments.SplashFragment"
android:label="SplashFragment"
tools:layout="@layout/layout_splash">
<action
android:id="@+id/action_splashFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true"/> // here to make home fragment as root
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name="com.example.***.fragments.HomeFragment"
android:label="HomeFragment"
tools:layout="@layout/layout_home">
</fragment>
</navigation>
and in SplashFragment
你只需要导航到主页OnCreate()
findNavController().navigate(R.id.action_splashFragment_to_homeFragment)
另外,如果您只想在代码中执行此操作
findNavController()
.navigate(R.id.action_splashFragment_to_homeFragment,
null,
NavOptions.Builder()
.setPopUpTo(R.id.splashFragment,
true).build()
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)