0

I have been trying to determine why the onPause() and the onResume() methods were not fired when the user switches the the tab to be displayed when using a ViewPager and sectionsPageAdapter.

I assumed that when the user changes the tab being displayed, the onPause() or the onDestroy() methods would be called. Then, if the user returns to the tab, the onResume() method would be called, but it seems it is not the functionality...

is there any way to determine when the user switces a tab and to know to which tab it has been switched? Something like onFragmentBeingDisplayed() or similar.

Thanks!

EDIT: Answer from Suraj Vaishnav

By editing the constructor from the FragmentPageAdapter like this super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); I could use the desired behaviour on the app

Ignasi
  • 601
  • 2
  • 10
  • 23

2 Answers2

1

That's because ViewPager loads some fragments on the left side and on the right side of the current fragment. By default, ViewPager loads one on the left side and one on the right side of the fragment. You can change the number via: viewpager.setOfScreenPageLimit(int);

What you thinking is working exactly the same way in androidx's Fragment. Means onPause and onResume will call accordingly. So the first suggestion is to use androix. Check this answer: https://stackoverflow.com/a/57886441/7682257

For some reason, If you do not want to use androidx then in your Fragment, override setUserVisibleHint method, which gives you isVisibleToUser as a parameter.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • Hi, my fragment class is already extending from the ```Fragment``` class from androidx ```import androidx.fragment.app.Fragment;```... and still the onPause and onResume methods are not called when I swipe or click to show the desired tab... could you tell me why? – Ignasi May 08 '20 at 09:40
  • You need to use FragmentPagerAdapter with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, as mentioned in this answer: https://stackoverflow.com/a/57886441/7682257, check it, it will help you surely – Suraj Vaishnav May 08 '20 at 09:43
  • 1
    Hi Sujaj! Indeed it worked! thank you very much. I edited the constructor so it used now: ```super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);``` and it worked as expected! Nevertheless, I realized that the ```FragmentPageAdapter```got deprecated... (https://developer.android.com/reference/androidx/fragment/app/FragmentPagerAdapter) I will have to give a look to how to use the new implementation. Anyway, thanks for your fast response and your help! – Ignasi May 08 '20 at 10:03
0

If you don't want to use Androidx version of Viewpager to get the use of the BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT behavior to fire onResume only on the displayed Fragment then you can instead add an addOnPageChangeListener

You can use the ViewPager.SimpleOnPageChangeListener or your own implementation of the ViewPager.OnPageChangeListener interface.

This PageChangeListener has a onPageSelected method with gets called with the position selected.

While I have given links to the Androidx Documentation this also works in earlier support libraries, just Google have removed the docs for it as they want you to use Androidx

Note this ViewPager.OnPageChangeListener onPageSelected fires at a slightly different time to onResume with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT. It fires when about 50% of the new tab is swiped on to the screen when onResume fires when 100% of the new tab is on screen.

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • Hi, the thing is that I am already using the androidx implementation... I already tried to use the ```addOnPageChangeListener``` approach, but I find it not as clean as it would be using the onPause and onResume methods of each of the fragments... that is the reason why I was asking. Do you know why if I am using the AndroidX it is not working? – Ignasi May 08 '20 at 09:44
  • As you worked out you were not creating the adapter with the right flags but without code examples in the question it was impossible to work this out. – Andrew May 08 '20 at 11:27