bovquier
bovquier

Reputation: 128

How to pop through fragment stack in NavController without loosing "parent" state

I open some main fragment FragA, which then can open other fragments, that are added to the stack, but when I press back I show a DialogC, which should clear the stack and get me back to FragA, without loosing it's state, restore it from stack, rather then creating it - to recreate it I'll have to pass some arg through the whole stack. I tried some configs with popUpTo in different places, and also used findNacController.popUpTo(with/out_aruments) or findNavController.navigate(R.id.action_dialog_c_to_frag_a) without destination defined in action, but pop can't find action in stack, navigate wants to recrete fragment when destination is defined, withou it cannot find pop action in stack (I/NavController: Ignoring popBackStack to destination frag_a as it was not found on the current back stack)

This is sample of my nav_graph:

<?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">
        <fragment
            android:id="@+id/asdf"
            android:name="SomeNaviFragment">
                <action
                    android:id="@+id/action_asdf_to_frag_a"
                    app:destination="@id/frag_A" />
        </fragment>
        <fragment
            android:id="@+id/frag_A"
            android:name="FragA">
        <argument
            android:name="some_id"
            app:argType="integer" />
        <action
            android:id="@+id/action_frag_a_to_frag_b"
            app:destination="@id/frag_B" />
        </fragment>
        <fragment
            android:id="@+id/frag_B"
            android:name="FragB">
        <action
            android:id="@+id/action_frag_b_to_dialog_frag_c"
            app:destination="@id/DialogFragC"/>
        </fragment>
        <dialog
            android:id="@+id/DialogFragC"
            android:name="DialogC">
                <action
                    android:id="@+id/action_dialog_c_to_frag_a"
                    app:popUpTo="@id/frag_a"
                    app:popUpToInclusive="true"/>
        </dialog>
</navigation>

In short - I wan't to go deeper from FragA through some fragments, but when the DialogC shows up in some point, I want to get back to FragA, to it's initial state. Is it possible to achive it without passing the creation arguments for FragA? Some solution already tried, like: Navigate Back with Navigation Component with it's linked resources, but this didn't help at all.

Upvotes: 0

Views: 2277

Answers (1)

Biscuit
Biscuit

Reputation: 5247

I've used the article in your post to make this https://github.com/yoobi/backNavigation I hope it helps.

EDIT: you're looking for a combination of this and the article then.

  • The popUpTo attribute of an action "pops up" the back stack to a given destination before navigating. (Destinations are removed from the back stack.)

  • If the popUpToInclusive attribute is false or is not set, popUpTo removes destinations up to the specified destination, but leaves the specified destination in the back stack.

  • If popUpToInclusive is set to true, the popUpTo attribute removes all destinations up to and including the given destination from the back stack.

  • If popUpToInclusive is true and popUpTo is set to the app's starting location, the action removes all app destinations from the back stack. The Back button takes the user all the way out of the app.

You can also check the count of your backstack with : parentFragmentManager.backStackEntryCount

Upvotes: 3

Related Questions