-1

I have a ViewPager which contain 2 tabs(2 Separate fragments).

My Second Tab consist of further 6 child tabs (each separate fragment) in another ViewPager.

Now When i switch between 6 child tabs, setUserVisibleHint() method is called.

But when i am present on my any of 6 child tabs(i.e fragment) and i try to switch between the 2 parent tabs. None of the methods like onPause(), onStop(), setUserVisibleHint(), and onHiddenChange() of child tab fragment do not get called.

So can you please help me to know which method will get called of child fragment when i switch between 2 parent tabs?

And if no methods will get called of child fragments, then how can i achieve or know that there is switch between parent tabs and call a method in child fragments?

Ankit Gupta
  • 674
  • 1
  • 6
  • 17

1 Answers1

0

You shouldn't rely on a fragment lifecycle in this case. It's controlled by adapter and if any of those callbacks gets called it doesn't guarantee you anything.

Use onPageChangeListener instead. You can set it on a ViewPager object via addOnPageChangeListener in onResume for example, and call removeOnPageChangeListener in onPause. It has a method onPageChanged which will be called every time you switch the page.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Thanks for your answer, But i do not get one thing that you asking me to set these mentioned listener , in Activity where i implemented ViewPager? Or Where? I have separate FragmentClass, separate Adapter Class and separate Activity class. – Ankit Gupta Jun 28 '16 at 07:10
  • Set the listener where you have a `ViewPager`, I believe you have it in your activity. If you need to access your fragment from the listener, use this as a reference: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager – Gennadii Saprykin Jun 28 '16 at 12:44