Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnResume called multiple times inside fragment

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();
like image 403
touchchandra Avatar asked Dec 14 '22 12:12

touchchandra


1 Answers

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();
like image 121
touchchandra Avatar answered Dec 21 '22 09:12

touchchandra