I am using NavigationUI of android architecture components with bottom navigation in my android app. I have the following situation
- The app starts at Fragment A
- Initial calculation is done and now I pop out Fragment A by
navController.popBackStack()
- The app goes to Fragment B (This serves as a 'Home' for the app)
- Now there are more fragments say C and D
- The user can navigate between B, C and D
Now the problem is in my navigation graph, my starting fragment was A (by defining app:startDestination="@id/fragmentA"
) but that is now pop out of the activity. Due to this, anytime if I backpress, the app just closes instead going back to the previous fragment. For example, let's say I navigate to Fragment C or D from fragment B and if I press back button, my app will close instead of going to fragment B. If the there way to 'reassign' the startDestination
?
I checked the answer to this question which has a similar situation as above but using NavOptions
is somehow not working. Is there any other way? Following is the code I used to pop fragment and add nav options
navController.popBackStack()
val navOptions: NavOptions = NavOptions.Builder()
.setPopUpTo(R.id.homeFragment, true)
.build()
navController.navigate(R.id.action_fragmentA_to_fragmentB, null, navOptions)