OnResume called multiple times inside fragment
Asked Answered
C

1

4

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();
Caul answered 22/8, 2015 at 14:10 Comment(0)
C
12

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();
Caul answered 23/8, 2015 at 3:39 Comment(1)
accepting my own answer because it worked for me and, may be helpful for others as well.Caul

© 2022 - 2024 — McMap. All rights reserved.