Where and how to populate my tab using fragment
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android, tabs are populated using Fragments by combining TabLayout with ViewPager2 and a FragmentStateAdapter. Each tab corresponds to a Fragment that defines its own layout and logic. The adapter maps tab positions to Fragment instances, and TabLayoutMediator connects the tab titles to the ViewPager pages. This pattern keeps each tab's UI isolated in its own Fragment, making the code modular and the lifecycle manageable.
Dependencies
Activity Layout
TabLayout renders the tab bar. ViewPager2 handles swiping between pages and hosts the fragments.
Fragment Adapter
FragmentStateAdapter creates and manages Fragment instances. createFragment() is called once per position — the adapter caches fragments and handles their lifecycle.
Activity Setup
TabLayoutMediator synchronizes the tab selection with the ViewPager page position and sets tab titles via the lambda.
Tab Fragments
Passing Data to Tab Fragments
Use Bundle arguments to pass data to fragments. Never pass data through constructor parameters — Android recreates fragments using the no-argument constructor.
Tabs with Icons
Using Fragment from a Parent Fragment
When tabs live inside a Fragment, pass the parent Fragment (not the Activity) to FragmentStateAdapter. This uses childFragmentManager so nested fragment lifecycles are managed correctly.
Common Pitfalls
- Using the deprecated
ViewPagerinstead ofViewPager2: The originalViewPagerandFragmentPagerAdapter/FragmentStatePagerAdapterare deprecated. UseViewPager2withFragmentStateAdapterfor proper lifecycle handling and RecyclerView-based performance. - Passing data through Fragment constructors: Android recreates Fragments using reflection with the no-argument constructor. Constructor parameters are lost. Always use
Bundlearguments viafragment.arguments = Bundle(). - Using
activity!!.supportFragmentManagerin nested fragments: When tabs are inside a Fragment, usechildFragmentManager(by passing the Fragment toFragmentStateAdapter). Using the activity's fragment manager causes lifecycle issues and crashes on configuration changes. - Not calling
.attach()onTabLayoutMediator: Forgetting.attach()means the tabs are not connected to the ViewPager. Tabs will not display titles and swiping will not update the selected tab. - Recreating fragments on every page switch:
FragmentStateAdapterhandles caching. Do not create new Fragment instances inonResumeor reinitialize the adapter on every lifecycle event. Set the adapter once inonCreateoronViewCreated.
Summary
- Use
ViewPager2+FragmentStateAdapter+TabLayoutMediatorfor tabbed interfaces - Each tab corresponds to a Fragment returned by
createFragment()in the adapter TabLayoutMediatorconnects tab titles/icons to ViewPager page positions- Pass data to fragments using
Bundlearguments, never constructor parameters - Use
childFragmentManager(pass the Fragment, not the Activity) when tabs are nested inside another Fragment - Call
.attach()onTabLayoutMediatorto activate the tab-page synchronization
Related reading
- Where are iOS simulator screenshots stored?
- Where can I find Android source code online?
- Where do alpha testers download Google Play Android apps?
- Where does Android Studio save the ProGuard mapping file?
- Where is debug.keystore in Android Studio
- Where should I be setting autolayout constraints when creating views programmatically
- Where to install Android SDK on Mac OS X?
- Where to place the 'assets' folder in Android Studio?
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my 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.