Navigation drawer How do I set the selected item at startup?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If your app opens with a navigation drawer, the highlighted drawer item should match the screen the user is actually seeing. In Android, the usual fix is to set the initial content first and then mark the corresponding menu item as checked with NavigationView.setCheckedItem(...).
The Basic Pattern
A NavigationView manages a menu, and one of those items can be checked at a time. The Android API exposes this directly through setCheckedItem.
In Kotlin:
That gives you the correct startup highlight and prevents replacing the fragment again after configuration changes.
Keep the UI State in Sync
Setting the checked item alone is not enough. The selected drawer item, the visible fragment, and the toolbar title should all point to the same destination.
A clean startup sequence is:
- decide which screen is the initial destination
- load that screen
- mark the matching drawer item as checked
- update any title or app bar state
If you skip step two and only call setCheckedItem, the drawer highlight lies to the user. If you skip step three, the content is right but the drawer looks wrong.
Manual Navigation Listener Example
If you manage drawer navigation manually, keep the listener and the startup state aligned:
Notice the symmetry: startup should use the same destination assumptions as the listener.
Alternative: Mark the Menu Item Directly
You can also access the MenuItem and mark it checked:
This works, but setCheckedItem(R.id.nav_home) is clearer because it communicates the intent directly and uses the dedicated API designed for the NavigationView.
If You Use the Navigation Component
In newer apps using the Jetpack Navigation component, the checked state is often driven by the current destination. In that setup, your real job is to navigate to the correct start destination and wire the NavigationView to the NavController.
Even then, the underlying rule stays the same: the highlighted drawer item should reflect the active destination, not some separate startup flag.
If you are not using NavController, then manual setCheckedItem is the direct answer.
Handling Restored State Correctly
A common bug is resetting the startup destination every time onCreate runs. On rotation or process recreation, the fragment manager may already be restoring the previous fragment. If you blindly replace it again, you can break back stack behavior or momentarily show the wrong screen.
That is why this guard matters:
Use it when setting the initial fragment and initial checked item.
Java Version
If your project is in Java, the idea is identical:
The important part is not the language. It is syncing the highlighted item with the destination the activity actually shows.
Common Pitfalls
The most common mistake is setting the checked drawer item without loading the corresponding fragment. That leaves the UI in an inconsistent state.
Another issue is doing the startup replacement unconditionally, which can override restored state on rotation and make the selected item jump unexpectedly.
Developers also sometimes mark the menu item checked inside the listener but forget to do the same during the initial launch path. The result is a correct highlight after user interaction, but no correct highlight on startup.
Finally, avoid duplicating navigation rules in several places. If startup picks one destination while the listener or NavController logic assumes another, the drawer state drifts out of sync.
Summary
- Use
navigationView.setCheckedItem(...)to highlight the startup drawer item. - Load the matching fragment or destination at the same time.
- Guard the initial setup with
savedInstanceState == null. - Keep the checked item, visible content, and title synchronized.
- Prefer the dedicated
setCheckedItemAPI over ad hoc menu-state manipulation when usingNavigationView.
Related reading
- NavigationBar bar, tint, and title text color in iOS 8
- NavigationBar bar, tint, and title text color in iOS 8
- NavigationLink Works Only for Once
- Need an explanation how to use AsyncTask?
- need some clarifications about dispatch queue, thread and NSRunLoop
- Need to use Apache Kafka in my React-Native app
- New Array from Index Range Swift
- New file created in Xcode 9.3, wsname.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist should it be committed?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.