android - Android导航组件 - 更改根片段?
问题描述
假设我有片段 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>
而在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()
)
推荐阅读
- sql - 如何修复:无法使用 Apache Drill SQL 表达式查询检索所有字段 MongoDB 集合
- python - 如何在 pandas fillna() 方法中为不同的列应用不同的方法参数
- ajax - 在 Laravel 5.8 上使用 Ajax 进行实时搜索并出现错误“500(内部服务器错误)”
- python - 如何将数据框中的一列转换为python中的二维数组
- java - javafx.fxml.LoadException:解析 onAction='#getText' 时出错,要么事件处理程序不在命名空间中,要么脚本中有错误
- python - Python返回neo4j的中间路径
- javascript - _ 和数组缩减的作用
- ruby - 给定 X 数和巨大的文件大小,我必须编写一个输出最大 X 数的程序
- javascript - 使用 chart.js 和关联数组生成条形图
- ocaml - 如何制作函数。在 Ocaml 中,它接受两个布尔值和一个 char(操作),然后返回相应的逻辑值