我正在使用导航控件(Android 架构)。我使用了一些演示并创建了导航架构。但问题是, fragment 每次都会被召回,就像:当你从任何 fragment 返回时替换 fragment 。那么我该如何阻止这种情况,以及如何设置添加 fragment 而不是替换 fragment 之类的行为。
我在网上发现了这个问题,但没有人告诉我如何在导航控件上添加/替换 fragment 。我尝试使用 fragment ID 来获得添加 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>
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/