Add fragment on navigation controller instead of replace fragment?
Asked Answered
H

3

20

I am using Navigation control (Android Architecture). I used some demo and creating navigation architecture. But the problem is, fragment is getting recalled every time just like: replace fragment when you back from any fragment. So how can I stop this, and how can I set behaviour like Add fragment instead of replace fragment.

I found this problem on web but no one is giving me idea that how to add/replace fragment on navigation control. I tried fragment id to get add fragment approach but not get success.

  1. This is my navigation host xml : I am adding one fragment again and again with logging life cycle.
<?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>
  1. I created one button, using this I call every time trigger the function and swap the fragment on host fragment :
val navController = Navigation.findNavController(activity!!,R.id.navhost_fragment)
                            navController.navigate(R.id.questionanswer)
        // using this i swap the fragment on host fragment.
Horbal answered 15/5, 2019 at 6:35 Comment(3)
Any solution found?Hughmanick
What is the actual problem you're having? If you're actually saving your state, then you're fragment will be in exactly the same state as it was in whether you use add or replace.Lapland
did you figure out a solution?Weise
M
0

Try this:

<action
    android:id="@+id/action_questionanswer_to_questionanswer"
    app:popUpTo="@+id/questionanswer"/>

instead of

<action
    android:id="@+id/action_questionanswer_to_questionanswer"
    app:destination="@id/questionanswer"/>

and then:

navController.navigate(R.id.action_questionanswer_to_questionanswer)

Found this here

Marxist answered 10/11, 2021 at 9:36 Comment(0)
P
0

Try this, without changing destination to popUpTop.

navController.navigate(R.id.action_questionanswer_to_questionanswer)
Parry answered 11/11, 2021 at 14:33 Comment(0)
O
0

Try deleting this line

val navController = Navigation.findNavController(activity!!,R.id.navhost_fragment)
                        navController.navigate(R.id.questionanswer)

and run your app again.

Ojeda answered 30/3, 2022 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.