Retrieve a Fragment from a ViewPager
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Retrieving a fragment from a ViewPager sounds simple, but the answer depends on how the adapter manages fragment instances. With ViewPager, fragments may be created, cached, restored, or destroyed by the adapter and fragment manager, so reaching for a fragment by position only works reliably if you understand that lifecycle.
The Most Practical Approach: Ask the FragmentManager
For classic ViewPager with FragmentPagerAdapter or FragmentStatePagerAdapter, the fragment manager stores fragments under a predictable tag pattern. That lets you retrieve the currently managed fragment by position.
This works because the pager adapter uses that naming convention internally for attached fragments.
Why Position Alone Is Not Enough
A common mistake is to keep a simple list of fragments and assume those references always match what the ViewPager is actually displaying. With FragmentStatePagerAdapter, off-screen fragments may be destroyed and recreated, so stale references can become misleading.
That is why querying the fragment manager is often more reliable than keeping your own unmanaged cache.
If You Control the Adapter, Track Fragments Carefully
Sometimes you do want the adapter to keep references intentionally.
If you do this, the cache must stay synchronized with the adapter lifecycle. Otherwise the references become incorrect.
Sometimes Retrieval Is the Wrong Design Goal
In many cases, directly grabbing a fragment is not the cleanest solution. Shared ViewModels, callbacks, fragment results, or adapter-driven state updates are often better patterns than reaching into the pager to manipulate a fragment instance directly.
If the need to "retrieve a fragment" comes from one screen wanting to push data into another screen, shared state or explicit communication channels usually scale better.
ViewPager2 Changes the Context
If the project uses ViewPager2, the adapter and fragment behavior differ, and old ViewPager advice does not always transfer directly. That is why the exact pager implementation matters before copying a fragment-retrieval snippet.
Retrieving the Current Page Is a Special Case
If you only need the fragment for the currently visible page, start from viewPager.currentItem and then ask the fragment manager for the tag tied to that position. That is often simpler than trying to maintain references for every page when the immediate need is only to interact with the active one.
Common Pitfalls
- Assuming fragment position maps permanently to one live fragment object.
- Keeping references without respecting
FragmentStatePagerAdapterlifecycle behavior. - Retrieving fragments directly when a shared ViewModel or result API would be cleaner.
- Mixing
ViewPagerandViewPager2solutions as if they were identical. - Debugging stale fragment references instead of asking the fragment manager for the active instance.
Summary
- The safest classic
ViewPagerretrieval technique is oftenFragmentManager.findFragmentByTag(...). - Adapter-managed fragment references can work, but only if they stay lifecycle-aware.
- Fragment destruction and recreation make naive position-based caching unreliable.
- Sometimes fragment retrieval is a design smell and shared state is the better solution.
ViewPagerandViewPager2should not be treated as interchangeable APIs.
Related reading
- Retrieving Android API version programmatically
- Retrofit 2.0 how to get deserialised error response.body
- Retrofit 2 - Dynamic URL
- Return multiple values from a function in swift
- Reverse engineering from an APK file to a project
- Reverse Range in Swift
- Right align text in android TextView
- Ripple effect on Android Lollipop CardView
.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.