Android 导航组件 - 更改根片段? [英] Android navigation component - change root fragment?
本文介绍了Android 导航组件 - 更改根片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有片段 a->b->c ,但是a"是启动画面,所以我希望b"成为堆栈中的第一个片段并永远抛出a",所以当我b"并按返回"系统按钮 - 我关闭应用程序
let's say I have fragments a->b->c , but "a" is a splash screen, so I want "b" to become first fragment in stack and throw "a" forever, so when I'm i "b" and press "back" system button - I close the app
在 SupportFragmentManager 中,我使用了 replace() 并且奏效了.但我怎么能在这里实现它?
In SupportFragmentManager I used replace() and that worked. But how can I achieve it here?
推荐答案
这里是一个例子
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"
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>
在 SplashFragment
中,您只需要在 OnCreate()
and in SplashFragment
you just need to navigate to Home in OnCreate()
findNavController().navigate(R.id.action_splashFragment_to_homeFragment)
如果您只想在代码中执行此操作
also if you want to do this in code only
findNavController()
.navigate(R.id.action_splashFragment_to_homeFragment,
null,
NavOptions.Builder()
.setPopUpTo(R.id.splashFragment,
true).build()
)
这篇关于Android 导航组件 - 更改根片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文