Software Development

What is the best way to trigger a data refresh in a ViewPager Fragment from its parent Activity?

DA Asked by Daniel Harris · 15-08-2025
0 upvotes 14,221 views 0 comments
The question

I am using a ViewPager2 with a FragmentStateAdapter to display several tabs of information. When a user updates their profile in the main Activity, I need to refresh the content inside the already-instantiated fragments. However, because ViewPager pre-loads fragments, my usual 'onResume' logic isn't firing when a user swipes back. Should I be using a ViewModel with LiveData, or is there a way to notify the adapter to recreate the fragments with new data?

3 answers

0
SA
Answered on 17-08-2025

The most modern and recommended approach is to use a Shared ViewModel scoped to the Activity. By having your fragments observe a LiveData or StateFlow object within that ViewModel, any change made by the Activity will automatically trigger the observer inside your fragments, even if they are currently off-screen but still in the ViewPager's memory. This avoids the messy process of trying to get a direct reference to a fragment instance through the adapter. If you absolutely must refresh the entire fragment set, you can override getItemItemId and containsItem in your FragmentStateAdapter and then call notifyDataSetChanged(). However, the ViewModel approach is much more efficient as it only updates the specific UI components that need changing rather than destroying and recreating the entire fragment view hierarchy.

0
JA
Answered on 19-08-2025

Using a Shared ViewModel is great for reactive updates, but if I have a very specific "refresh" button in my Fragment, should I still route that through the Activity's ViewModel or can the fragment handle its own local data fetch independently?

TH 21-08-2025

If the refresh is local to that specific tab, the Fragment can definitely handle its own data fetch. However, keep in mind that if that data affects other tabs, you’ll still want to sync it through the shared ViewModel. A common pitfall is fetching data in onCreateView, which might only happen once. If you want the refresh to happen every time the user swipes to that tab, you should look into the onResume override or, in ViewPager2, use a lifecycle observer to detect when the fragment becomes the primary item.

0
LI
Answered on 10-09-2025

You can use adapter.notifyDataSetChanged() but make sure your adapter's createFragment logic is set up to pull the latest data. It's the "brute force" method but it works for simple apps.

DA 12-09-2025

Linda is right, but just a warning: notifyDataSetChanged in ViewPager2 can be tricky. You must ensure you've overridden the item ID methods, otherwise, the adapter might think the items haven't changed and won't refresh the view, leaving you with stale data.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session