Same Navigation Drawer in different Activities
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When an Android app still uses multiple activities, keeping one consistent navigation drawer across screens prevents duplicated code and navigation bugs. Without a shared pattern, menu handling, checked states, and toolbar behavior drift over time. A robust solution centralizes drawer setup in reusable infrastructure and keeps each activity focused on screen content.
Use One Shared Drawer Shell Layout
Create one layout that contains DrawerLayout, NavigationView, and a content container. Each activity then inflates its own content view into that container.
This guarantees visual consistency and keeps menu IDs stable.
Centralize Logic in a Base Activity
A base class should own drawer wiring, toolbar toggle, and menu routing.
Each child activity only declares content and active menu item.
Keep Back Stack and State Predictable
Drawer navigation should usually behave like top-level navigation. Avoid stacking repeated activity instances.
Recommended behavior:
- if target screen is already active, do nothing,
- if switching top-level screens, optionally call
finishto keep stack clean, - preserve current drawer selection after configuration changes.
Also save and restore key UI state in each activity as needed, especially when forms exist inside content screens.
Menu Resource Strategy
Use one canonical menu file shared by all activities.
Do not create per-activity variants unless product requirements demand role-based visibility. If visibility changes by role, keep IDs identical and toggle visibility in one shared function.
Testing Recommendations
High-value tests for this pattern:
- selecting each menu item routes to expected activity,
- current item is highlighted correctly,
- repeated tap on current destination does not duplicate screen,
- drawer opens and closes with toolbar toggle.
UI tests with Espresso can validate route behavior. Unit tests can validate route mapping logic in pure Kotlin helpers.
Migration Path Toward Single-Activity Navigation
If your roadmap includes Jetpack Navigation with fragments, this base-activity drawer design still helps. You can keep menu definitions and route contracts stable while gradually replacing activity targets with navigation actions.
A staged migration avoids big-bang rewrites:
- keep existing activity routes working,
- introduce fragment destinations for new screens,
- move one menu item at a time to NavController handling.
This preserves user experience consistency while reducing architecture risk.
Common Pitfalls
- Copying drawer setup into every activity and drifting behavior.
- Reusing menu labels but changing item IDs across screens.
- Launching same activity repeatedly and polluting back stack.
- Updating toolbar settings in child classes inconsistently.
- Forgetting to synchronize drawer toggle after setup.
Summary
- Use one drawer shell layout and one shared menu definition.
- Centralize drawer setup and routing in a base activity.
- Keep child activities focused on content and selected menu ID.
- Enforce predictable top-level navigation and back stack behavior.
- Test routing, highlighting, and duplicate-launch prevention explicitly.
Related reading
- Sandbox bash72986 deny1 file-write-data /Users/XXX/ios/Pods/resources-to-copy-XXXShareExtension.txt
- Save An Image To Application Documents Folder From UIView On IOS
- Save and Load from KeyChain Swift
- Save ArrayList to SharedPreferences
- Save custom objects into NSUserDefaults
- Save custom objects into NSUserDefaults
- Save images in NSUserDefaults?
- Save sensitive data in React Native
.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.