codelab-android-navigation
codelab-android-navigation copied to clipboard
Disable multiple backstack
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 did you find any way to disable it?
@similincbose Unfortunately no.
@Hussienfahmy hey got it
binding.bottomnavigationbar.apply {
setupWithNavController(navController)
setOnItemSelectedListener { item ->
NavigationUI.onNavDestinationSelected(item, navController)
navController.popBackStack(item.itemId, inclusive = false)
true
}
}
@similincbose Can you mention some references regarding this because i want Java version of the above code.
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; } });
邮件已经收到了,谢谢!
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
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.
邮件已经收到了,谢谢!
- 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.
-
If you specify
app:popUpTo="@id/home_navigation"
, then destinations in the back stack will get popped off until you reach home_navigation. -
and you must add
app:popUpToInclusive="true"
.to have just one new instance of StartFragment in the back stack.
邮件已经收到了,谢谢!