问题
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