Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Backstack status using android navigation component

I want to implement backpress behaviour such that it prompts a confirmation popup when you press back with the backstack empty, otherwise it pops the next fragment in the stack.

I'm trying to get the backstack count, but i always get 0 from both fragment managers

 getSupportFragmentManager().getBackStackEntryCount();
 getFragmentManager().getBackStackEntryCount();

I guess it should work since i checked the google code of fragment navigator and it adds to backstack through the canonical fragment transaction:

FragmentNavigator.java:

 if (initialNavigation || isClearTask) {
        backStackEffect = BACK_STACK_DESTINATION_ADDED;
    } else if (isSingleTopReplacement) {
        // Single Top means we only want one instance on the back stack
        if (mBackStack.size() > 1) {
            // If the Fragment to be replaced is on the FragmentManager's
            // back stack, a simple replace() isn't enough so we
            // remove it from the back stack and put our replacement
            // on the back stack in its place
            mFragmentManager.popBackStack();
            ft.addToBackStack(Integer.toString(destId));
            mIsPendingBackStackOperation = true;
        }
        backStackEffect = BACK_STACK_UNCHANGED;
    } else {
        ft.addToBackStack(Integer.toString(destId));
        mIsPendingBackStackOperation = true;
        backStackEffect = BACK_STACK_DESTINATION_ADDED;
    }
    ft.setReorderingAllowed(true);
    ft.commit();

I didn't find any API to retrieve this information through NavController or Navigator neither.

Thanks

like image 205
Fabio Avatar asked Sep 24 '18 14:09

Fabio


People also ask

How do I get NavController in fragment?

To retrieve the NavController for a fragment, activity, or view, use one of the following methods: Kotlin: Fragment. findNavController()

How do you complete a fragment navigation component?

How do I get rid of current fragment navigation component? You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods: FragmentManager manager = getActivity(). getSupportFragmentManager();

How do I go back to previous fragment in navigation component?

Use Up or Back button to go to a previous step of the order flow.

What is navigateUp?

navigateUp() Attempts to navigate up in the navigation hierarchy. boolean.


2 Answers

May be help to someone too:

<fragment
    android:id="@+id/YOUR_NAVIGATION_HOST_ID"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/YOUR_NAVIGATION_HOST" />
val navHostFragment = supportFragmentManager.findFragmentById(R.id.YOUR_NAVIGATION_HOST_ID)
val backStackEntryCount = navHostFragment?.childFragmentManager?.backStackEntryCount
like image 151
damienG Avatar answered Sep 17 '22 15:09

damienG


To check the back stack count of a navigation host fragment, you should use childFragmentManager of navHostFragment :

navHostFragment.childFragmentManager.addOnBackStackChangedListener {
            if (navHostFragment.childFragmentManager.backStackEntryCount == 0) {
                // First fragment is open, backstack is empty
            } else {
                // there are fragments in backstack
            }
        }
like image 31
Ana Koridze Avatar answered Sep 17 '22 15:09

Ana Koridze