android - 禁止在导航架构组件中将 fragment 添加到后台堆栈

标签 android android-architecture-components android-jetpack android-architecture-navigation

假设我有 4 个 fragment :A、B、C、X,我可以通过这种方式在它们之间导航:

... -> A -> C -> X    and ... -> B -> C -> X

但是当我在 fragment X 中调用 mNavController.navigateUp() 时,我想跳过 fragment C 并转到 fragment A 或 B。我需要做什么?

更新:我只需要导航架构组件的解决方案https://developer.android.com/topic/libraries/architecture/navigation/ 谢谢!

最佳答案

或者,您可以在导航 xml 资源中使用 app:popUpToapp:popUpToInclusive 属性在执行某些事务时自动清理返回堆栈,所以后退/向上按钮会将您的用户带到根 fragment 。

<fragment
    android:id="@+id/fragment1"
    android:name="com.package.Fragment1"
    android:label="Fragment 1">

    <action
        android:id="@+id/action_fragment1_to_fragment2"
        app:destination="@id/fragment2"
        app:popUpTo="@id/fragment1"
        app:popUpToInclusive="true / false" />

</fragment>

关于android - 禁止在导航架构组件中将 fragment 添加到后台堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533721/

相关文章:

android - 应用程序注册 ID 未在 GCM Android 中生成

android - 无法创建 ViewModel 类的实例(无法启动 Activity ComponentInfo)

android - DAO 错误 :-Type of the parameter must be a class annotated with @Entity or a collection/array of it

android - 撰写错误 - 导入后 "Unresolved reference: align"

安卓相对布局

android - Unity Android Build错误:无法读取PNG签名:文件不是以PNG签名开头

android - 带有嵌入式浏览器的 Google OAuth 2.0 和强制门户

android - 使用单个 NavHostFragment 导航具有多个模块的应用程序

android - java/kotlin 模块中的 Jetpack Paging 3

android - 无法使用转换 JetifyTransform 解决依赖关系?