I have a List of fragments which i can open in my navbar with:
getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, l_Fragment).commit();
The fragments are all from the same type -> CDMBasicMovieFragment extends Fragment
Inside this fragment i have a viewpager which i call with getFragmentManager()
. And here i have the problem. The first fragment is fine but after replacing the first fragment, the viewpager is empty. Then i changed getFragmentManager()
to getChildFragmentManager()
. Now all fragments are fine. But only 1 time. After replacing back to the first fragment i have the same problem. What am i doing wrong?
Edit1:
My view hierarchy:
Activity
- CDMBasicMovieFragment 1
- ViewPager
- CDMMovieListFragment
- ViewPager
- CDMBasicMovieFragment 2
- ViewPager
- CDMMovieListFragment
- ViewPager
- CDMBasicMovieFragment 3
- ViewPager
- CDMMovieListFragment
- ViewPager
- CDMBasicMovieFragment 1
Edit2:
I tried to create unique ids for the fragments and used them on replace but i already have the same problem:
getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, l_Fragment, uniqueFragmentTag).commit();
Edit3:
I found a possible solution: OnCreateView called multiple times / Working with ActionBar and Fragments I tried everything from this link. I already have the same problem... Can anyone help?
UPDATE:
After a intensive debug session ;) i found this:
When loading the first CDMMovieListFragment
FragmentPagerAdapter's instantiateItem
is called and checks if there's already a fragment. It's not thats why it internally calls
mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId));
When i replace the CDMBasicMovieFragment
which includes the fragmentpageradapter (inside is the added CDMMovieListFragment
) and try to load the second CDMBasicMovieFragment
then it's still the same fragmentmanager.
FragmentPagerAdapter's instantiateItem
is called and found the old CDMMovieListFragment
. Now it calls mCurTransaction.attach(fragment);
instead. I think here's the problem. But im so confused...