Reputation: 144
I am setting the current tab in my activity from viewPager.setCurrentItem(position), this perfectly opens the desired fragment but I want to know which function of fragment is called when setting currentItem from viewPager.I think onResume() should have been called after reading fragments lifecycle but actually onResume() is not called.
My Activity Code:
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Code to select tab:
viewPager.setCurrentItem(position);
Upvotes: 0
Views: 1336
Reputation: 3522
If you're using FragmentPagerAdapter
, then when you invoke setCurrentItem()
, the fragment's view is already cached in memory by the viewpager, so no lifecycle calls will be made on the fragment when you switch between items. (Viewpager already instantiated the fragment and went through calling its onCreate, onCreateView, onStart, onResume).
Viewpager has a method called setOffscreenPageLimit()
which controls how many fragments will be cached in the view hierarchy. Checkout this android doc if you wanna tune it.
If you want your fragments destroyed and re-created as you page between them, checkout FragmentStatePagerAdapter. It's made more for that.
If you just need to react to viewpager's changes, do it in viewpager's onPageSelected()
.
Upvotes: 3