Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Disable multiple backstack #115

Open
Hussienfahmy opened this issue Sep 16, 2021 · 11 comments
Open

Disable multiple backstack #115

Hussienfahmy opened this issue Sep 16, 2021 · 11 comments

Comments

@Hussienfahmy
Copy link

Hello I have an inquiry,
Mr. Murat Yener mentioned here that multiple backstack is enabled by default, but he didn't mention how to disable it
How I can disable Multiple backstack

@similincbose
Copy link

@Hussienfahmy did you find any way to disable it?

@Hussienfahmy
Copy link
Author

@similincbose Unfortunately no.

@similincbose
Copy link

@Hussienfahmy hey got it

binding.bottomnavigationbar.apply {
            setupWithNavController(navController)
            setOnItemSelectedListener { item ->
                NavigationUI.onNavDestinationSelected(item, navController)
                navController.popBackStack(item.itemId, inclusive = false)
                true
            }
        }

@rishabh-hk
Copy link

@similincbose Can you mention some references regarding this because i want Java version of the above code.

@rishabh-hk
Copy link

Got it .. Thanks :)

Java Code will be:-

bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@OverRide
public boolean onNavigationItemSelected(@nonnull MenuItem item) {
NavigationUI.onNavDestinationSelected(item,navController);
navController.popBackStack(item.getItemId(),false);
return true;
}
});

@mtf7101520
Copy link

mtf7101520 commented Mar 21, 2022 via email

@OndraBasler
Copy link

OndraBasler commented Jul 7, 2022

I think I have found a better way how to disable it. Just call onNavDestinationSelected with false:

binding.bottomnavigationbar.apply {
            setupWithNavController(navController)
            setOnItemSelectedListener { item ->
                NavigationUI.onNavDestinationSelected(item, navController, false)
                true
            }
        }

I use 2.5.0 version of navigation components

@Al-Hussein-96
Copy link

Al-Hussein-96 commented Aug 2, 2022

Update:
this cause a new behavior for on back press, it created a new back stack fro bottom navigaiton items

I found this soultion as NavigationUI.kt

By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.

so the final solution work for me on 2.5.0 vesion is:

    <item
        android:id="@+id/parcel_navigation"
        android:icon="@drawable/ic_home_parcel"
        android:menuCategory="secondary"
        android:title="@string/Parcels"
        app:showAsAction="always" />

But I had to add it to all menu to do the correct behavior of bottom sheet.

@mtf7101520
Copy link

mtf7101520 commented Aug 2, 2022 via email

@ybitite
Copy link

ybitite commented Jan 4, 2023

  • Activities are arranged in a stack.
  • Where each new activity you visits gets pushed onto the back stack.
  • When you were on return the stacks are visited one after the other.
  • But when you navigate via an action you can delete all stack that you dont need.
  • For that you can add app:popUpTo="@id/your_id" on the appropriate navigation actions.
  1. If you specify app:popUpTo="@id/home_navigation", then destinations in the back stack will get popped off until you reach home_navigation.

  2. and you must add app:popUpToInclusive="true" .to have just one new instance of StartFragment in the back stack.

@mtf7101520
Copy link

mtf7101520 commented Jan 4, 2023 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants