Switch to ViewPager2 and use FragmentStateAdapter instead.
From there you can use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume is called when a fragment became visible and onPause when it stops to be visible. -> read more
Based on the comments of Eric and Reejesh.
Old answer (deprecated too now)
The following constructors do the same
super(@NonNull FragmentManager fm)
super(@NonNull FragmentManager fm, BEHAVIOR_SET_USER_VISIBLE_HINT)
Passing BEHAVIOR_SET_USER_VISIBLE_HINT got deprecated. You should pass BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT instead.
The difference in passing those is explained in FragmentPagerAdapter:
/**
* Indicates that Fragment#setUserVisibleHint(boolean) will be
* called when the current fragment changes.
*/
@Deprecated
public static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0;
/**
* Indicates that only the current fragment will be
* in the Lifecycle.State#RESUMED state. All other Fragments
* are capped at Lifecycle.State#STARTED.
*/
public static final int BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT = 1;