Situation:
My app is designed to operate in the following way-
1. On fresh install:
OpeningFragment -> Fragment1 -> Fragment2
2. Restarting the app, after closing it in Fragment2:
OpeningFragment -> Fragment2
In the second situation I want Fragment1 to be in the back stack, so that the up or back button from Fragment2 will take me to Fragment1 and not OpeningFragment.
Approaches I have tried:
1. Using consecutive navigate() from OpeningFragment:
findNavController().navigate(OpeningFragmentToFragment1)
findNavController().navigate(Fragment1ToFragment2)
Problem: While this works fine and gives the desired result, I am not sure if there are any potential bugs to this approach. Would it lead to app crashes? Is it dangerous to invoke the Fragment1ToFragment2 action from OpeningFragment instead of Fragment1?
2. Using Deep Link:
nav_main_graph: OpeningFragment (startDestination)
nav_deepLink_graph: Fragment1 (startDestination) -> Fragment2 {deeplink}
i.
findNavController().navigate(Uri.parse(deepLinkUrl))
Problem: This takes me to the appropriate fragment but pressing both the up and back button keeps reloading this fragment instead of quitting the app or going back to at least OpeningFragment.
ii.
val intent = Intent()
intent.data = Uri.parse(deepLinkUrl)
findNavController().handleDeepLink(intent)
Problem: This also takes me to the appropriate fragment but pressing the back button quits the app and pressing the up button takes me to Fragment1, but flashes OpeningFragment for a second. I want both back and up button to act exactly the same: Just take me to Fragment1, without any unwanted flashes.
My request: I would appreciate it if somebody could help me solve the problems mentioned above or guide me to a more appropriate solution to achieve the desired result. Thank you.
Some of the links that I have already checked and tried:
- Android Navigation library deep linking: How to synthesise backstack
- https://issuetracker.google.com/issues/79734195
- https://medium.com/swlh/proper-back-stack-on-android-every-time-4a811f8ab78c
- Android Developer Guidelines on Navigation Components and Deep Link (Explicit and Implicit)
I apologise for not being able to share code snippets due to confidentiality reasons.