2

The default behavior of Bottom Nav View when pressing back button is that navigation returns to home fragment and pressing back again quits the application however, when the home fragment is not the root fragment in navigation the issues arise. E.g. If login screen is before the main screen (which is with bottom nav view), after navigation to main screen, every tab navigation is placed in stack which is very weird. Even if you try to remove login screen in navigation graph using popUpTo and popUpToInclusive, nothing changes. Any suggestions?

6
  • 1
    Your login screen should probably just be a separate Activity anyways. That will avoid the whole problem. Commented Jun 9, 2022 at 21:30
  • The Principles of Navigation specifically state that login should never, ever be the start destination of your graph and the user login guide specifically does not use that approach. Is there a reason you aren't following the guidance specifically for this case? Commented Jun 9, 2022 at 23:05
  • @GavinWright Yes, creating another activity makes for us possible to avoid this problem, but I wish to solve the problem using sole activity. Commented Jun 10, 2022 at 9:08
  • 1
    @ianhanniballake I am using entry fragment for navigation. I check user login in entry fragment which opens either login or main screen. It causes the aforementioned problem to spoil my app. How can I solve it exactly? I have read that doc but I think it does not suit my case Commented Jun 10, 2022 at 9:15
  • It is not just about login screen. What if I want to make boarding screen before main screen with bottom nav? Commented Jun 10, 2022 at 9:38

1 Answer 1

2

every tab navigation is placed in stack which is very weird

This is a consequence of the setupWithNavController implementation. It's rather simple:

  • When an item is selected on the navigation bar:
    • Pop the current backstack up to, and excluding, the start destination of the navigation graph (1)
    • Navigate to the menu item's corresponding destination
  • When the NavController changes destination:
    • Identify the corresponding menu item, by matching the destination ID with a menu item's ID (which is also a destination's ID)
    • Simply set the identified item as selected

The navigation unexpectedly beginning to stack is a result of your backstack being out of sync with the expectations of the setupWithNavController implementation as per (1). Namely, if the start destination of the graph does not exist anywhere within your backstack, the current tab's backstack won't be popped up, resulting in tabs eventually having stacked on top of each other.

Hence, you should be fine so long as you make sure to always keep the start destination of your navigation graph somewhere reasonable within the backstack. With relation to your particular situation, it's also a good idea to have the default tab on your navigation bar as the start destination of the navigation graph that the navigation bar's set up NavController is using.

P.S. Google should've at least touched on this requirement within the documentation for setupWithNavController. I quite dislike magical black box implementations such as this one when there is a clear risk of misuse.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.