ViewPager with fragments - onPause(), onResume()?

37,609

Considering previous solutions are not very clear, here is how I solved it thanks to Phix for his hint:

In the OnPageChange() callback:

    Fragment old_fragment = getActiveFragment(old_position);
    Fragment new_fragment = getActiveFragment(new_position);
    old_f.setUserVisibleHint(false);
    old_f.onPause();
    new_f.setUserVisibleHint(true);
    new_f.onResume();

Then in onResume():

    if (getUserVisibleHint())
        /* resume code */

and in onPause():

    if (!getUserVisibleHint())
        /* pause code */

Here is the getActiveFragment code, avoiding to search for that too:

    return fragmentManager.findFragmentByTag("android:switcher:" + viewPagerId + ":" + position);

NB: Fragment have a isVisible() method, but it doesn't seem to be very consistent, hence the use of UserVisibleHint.

EDIT: Replace the UserVisibleHint by a private flag. Worked much better!

Share:
37,609
user291701
Author by

user291701

Updated on January 20, 2022

Comments

  • user291701
    user291701 over 2 years

    When using ViewPager with fragments, our onPause, onResume methods are not called when moving between tabs. Is there any way we can figure out in the fragment when we're being made visible or being hidden?

    Unfortunately I have logic in onResume, onPause, like registering with location services, that never get stopped when switching tabs because onPause never gets called until one exits the whole app.

  • 3c71
    3c71 over 9 years
    Sure it's not a good solution, but there's no other at the moment to work-around this OS "bug" or behavior. Why fragments have onResume() onPause() but it's never called in the first place?
  • Emmanuel
    Emmanuel over 9 years
    It is not a OS bug. This means the Fragment is still in memory. ViewPager will retain in memory at least 2 Fragments do to its paging nature. Look at this answer for more details.
  • 3c71
    3c71 over 9 years
    Hence I also said "behavior". Note sure what your comment or link you provide add to the discussion though. The problem is that one need a way to know when the fragment is shown/hidden, which I implemented more or less this way, except I call some private onVisible and onHidden methods. If you know of a better solution, why not write your own? The only 2 answers to this question are more or less the same.
  • 3c71
    3c71 over 9 years
    And it's definitely a bug, if you take a look at Fragment reference (developer.android.com/reference/android/app/Fragment.html). basically, onStart() makes the fragment visible to the user, onResume() makes the fragment interacting with the user, onPause() fragment is no longer interacting with the user, onStop() fragment is no longer visible to the user. Those are only called when the fragments are created, which have nothing to do with them being visible.
  • Dave S
    Dave S over 9 years
    The proper solution would be to perform the logic in the OnPageChangeListener. onPause and onResume are for when the activity moves in and out of the background not for visibility changes.