codelab-android-navigation icon indicating copy to clipboard operation
codelab-android-navigation copied to clipboard

Disable multiple backstack

Open Hussienfahmy opened this issue 3 years ago • 11 comments

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

Hussienfahmy avatar Sep 16 '21 13:09 Hussienfahmy

@Hussienfahmy did you find any way to disable it?

similincbose avatar Jan 03 '22 17:01 similincbose

@similincbose Unfortunately no.

Hussienfahmy avatar Jan 03 '22 17:01 Hussienfahmy

@Hussienfahmy hey got it

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

similincbose avatar Jan 03 '22 18:01 similincbose

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

rishabh-hk avatar Mar 21 '22 11:03 rishabh-hk

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; } });

rishabh-hk avatar Mar 21 '22 12:03 rishabh-hk

邮件已经收到了,谢谢!

mtf7101520 avatar Mar 21 '22 12:03 mtf7101520

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

OndraBasler avatar Jul 07 '22 08:07 OndraBasler

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.

Al-Hussein-96 avatar Aug 02 '22 09:08 Al-Hussein-96

邮件已经收到了,谢谢!

mtf7101520 avatar Aug 02 '22 09:08 mtf7101520

  • 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.

ybitite avatar Jan 04 '23 02:01 ybitite

邮件已经收到了,谢谢!

mtf7101520 avatar Jan 04 '23 02:01 mtf7101520