OnResume called multiple times inside fragment

怎甘沉沦 提交于 2020-01-24 10:33:06

问题


I have InnerFragment containing RecyclerView, inside MainFragment which is added to ViewPager. When MainFragment gets created while swiping OnResume is called multiple times

  • 1st time : OnResume called once in MainFragment and Once in InnerFragment
  • 2n time : OnResume called once in MainFragment and Twice in InnerFragment
  • 3rd time : OnResume called once in MainFragment and Thrice in InnerFragment

Adding MainFragment in ViewPager

//Assigning Adapter in ViewPager
mViewPager.setAdapter(new MyAdapter(getFragmentManager()));
//getItem inside MyAdaper
public Fragment getItem(int position){
    return MainFragment.newInstance(position);
}

Adding InnerFragment in MainFragment

getChildFragmentManager().beginTransaction().add(R.id.framelayoutID, InnerFragment.newInstance()).commit();

回答1:


when adding fragment, use replace() instead of add()

before

getChildFragmentManager().beginTransaction().add(R.id.framelayoutID, InnerFragment.newInstance()).commit();

after

getChildFragmentManager().beginTransaction().replace(R.id.framelayoutID, InnerFragment.newInstance()).commit();


来源:https://stackoverflow.com/questions/32157061/onresume-called-multiple-times-inside-fragment

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