Fragment onResume() isn't called when using FragmentPagerAdapter

心不动则不痛 提交于 2019-12-03 09:12:46
ondrej kosatka

I had the same problem while ago.

Create a new interface and implement it by both of your Fragments:

public interface OnPageSelectedListener {
    void onPageSelected();
}

In parent activity implement android.support.v4.view.ViewPager.OnPageChangeListener and call Fragment method like this:

@Override
public void onPageSelected(int i) {
    OnPageSelectedListener fragment = (OnPageSelectedListener ((PlaceListPagerAdapter)pager.getAdapter()).getFragment(i);
    fragment.onPageSelected();
}

PS: Name of the new interface and its method is a bit confusing, so be careful or change it.

Override Fragment.setUserVisibleHint. When setUserVisibleHint is true call same logic you use for onResume.

You can see when FragmentPagerAdapter calls setUserVisibleHint in instantiateItem and setPrimaryItem. Applicable to android since 4.2.1 and support-v4.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!