android - 在导航 Controller 上添加 fragment 而不是替换 fragment ?

标签 android android-fragments navigationcontroller

我正在使用导航控件(Android 架构)。我使用了一些演示并创建了导航架构。但问题是, fragment 每次都会被召回,就像:当你从任何 fragment 返回时替换 fragment 。那么我该如何阻止这种情况,以及如何设置添加 fragment 而不是替换 fragment 之类的行为。

我在网上发现了这个问题,但没有人告诉我如何在导航控件上添加/替换 fragment 。我尝试使用 fragment ID 来获得添加 fragment 方法,但没有成功。

  • 这是我的导航主机 xml:我一次又一次地添加一个 fragment
    具有日志记录生命周期。

  • <?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/nav_graph"
                app:startDestination="@id/questionanswer">
    
                <fragment
                    android:id="@+id/questionanswer"
                    android:name="com.softtech360.totalservey.fragment.QuestionAnswer"
                    android:label="fragment_first"
                    tools:layout="@layout/questionanswer" >
                    <action
                        android:id="@+id/action_questionanswer_to_questionanswer"
                        app:destination="@id/questionanswer"
    
                        />
                </fragment>
    </navigation>
    
  • 我创建了一个按钮,每次触发该功能并在主机 fragment 上交换 fragment 时都使用此按钮:

  • val navController = Navigation.findNavController(activity!!,R.id.navhost_fragment)
                                navController.navigate(R.id.questionanswer)
            // using this i swap the fragment on host fragment.
    

    最佳答案

    尝试这个:

    <action
        android:id="@+id/action_questionanswer_to_questionanswer"
        app:popUpTo="@+id/questionanswer"/>
    
    代替
    <action
        android:id="@+id/action_questionanswer_to_questionanswer"
        app:destination="@id/questionanswer"/>
    
    接着:
    navController.navigate(R.id.action_questionanswer_to_questionanswer)
    
    找到这个 here

    关于android - 在导航 Controller 上添加 fragment 而不是替换 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56143041/

    相关文章:

    android - Android 富文本编辑器中的 CSS 样式不起作用

    java - 标记上存在语法错误,需要 AnnotationName - 查询错误

    iphone - 从 subview 导航 Controller 访问顶部导航 Controller

    objective-c - 导航 Controller 问题

    swift - 滑动以在 Swift 中弹出 View Controller

    java - 将 ImageButton 添加到 ListView

    android - 从编辑文本中获取十进制数

    java - 如何制作 GridView 项目( TextView )的 .setText() onItemSelected?

    android - fragment 未附加到上下文

    android - 带有 GridLayoutManager 水平滚动的 Recycler View 不起作用