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

user291701 picture user291701 · Jun 1, 2012 · Viewed 35.2k times · Source

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.

Answer

Phix picture Phix · Jun 2, 2012

The ViewPager comes with the OnPageChangeListener interface. By setting some flags for the previous and currently shown pages, you can emulate this behavior.