android / codelab-android-navigation

Apache License 2.0
634 stars 275 forks source link

Disable multiple backstack #115

Open Hussienfahmy opened 3 years ago

Hussienfahmy commented 3 years ago

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 commented 2 years ago

@Hussienfahmy did you find any way to disable it?

Hussienfahmy commented 2 years ago

@similincbose Unfortunately no.

similincbose commented 2 years ago

@Hussienfahmy hey got it

binding.bottomnavigationbar.apply {
            setupWithNavController(navController)
            setOnItemSelectedListener { item ->
                NavigationUI.onNavDestinationSelected(item, navController)
                navController.popBackStack(item.itemId, inclusive = false)
                true
            }
        }
rishabh-hk commented 2 years ago

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

rishabh-hk commented 2 years ago

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 commented 2 years ago

邮件已经收到了,谢谢!

OndraBasler commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

邮件已经收到了,谢谢!

ybitite commented 1 year ago
  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 commented 1 year ago

邮件已经收到了,谢谢!