Is There A Way How To Navigate To Destination And Add Another To Backtstack In NavigationManager?
Hi I am new to android NavigationManager.
Consider fragments A, B and C conected in nav_graph as shown in the picture.
Basically what I am trying to achieve is to navigate from fragment A directly to C. But when clicking on back button, I want it to behave as if I were first in fragment B.
So A->C, on back buttons: C->B->A
Is there some way, how I could add fragment B to backstack without actually showing it using NavigationManager?
Answer
So it seems I should be more careful when reading documentation. I missed this site.
There is mechanism for custom back behavior. All I needed to do was to add this code to fragment C class:
//use onResume if you want to handle orientation change
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this, object :
OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val options = NavOptions.Builder()
.setPopUpTo(R.id.fragmentC, true)
.build()
navController.navigate(R.id.fragmentB, options)
}
})
}
My mindset was wrong the whole time. I was trying to add B to backstack when launching C. What I had to do instead in fragment C on back button pressed was going forward to B and delete C from backstack.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View