Search code examples
androidandroid-fragmentsandroid-viewpagerfragment-lifecycle

Where to put "ViewPager.setCurrentItem()" Method


I have Nested Fragments [1 main Fragment called "MainFrag" and many child Fragments] the App can show MainFrag or can show other fragments.

what I want to do is to setCurrentItem of MainFrag's ViewPager every time the user selects to show that MainFrag so I tried to put it at it's onCreateView but this only works for the first time the User Shows MainFrag as if user Selects another fragment and then re-selects MainFrag the seCurrentItem is not working

I also tried to put it inside MainFrag's onResume, It worked as required but it also causes a problem that if the user moved the App to background and then reOpen it viewPager will Scroll and i don't want this

So where Exactly Can I put setCurrentItem ?

mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);

Edit: I am using Navigation Menu to Move between 3 main fragments where MainFrag is one of them.


Solution

  • After your mViewPager.setAdapter(adapter); method you can put

    mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);