1

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
    • CDMBasicMovieFragment 2
      • ViewPager
        • CDMMovieListFragment
    • CDMBasicMovieFragment 3
      • ViewPager
        • CDMMovieListFragment

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...

Community
  • 1
  • 1
beeb
  • 1,615
  • 1
  • 12
  • 24
  • Is this relevant to your situation? mViewPager = (ViewPager) findViewById(R.id.pager); // must have OffscreenPageLimit set because default is 1 and we have 3 tabs. If one is not available off screen when a loader finishes it will crash otherwise mViewPager.setOffscreenPageLimit(2); – usajnf Aug 10 '16 at 22:58
  • I dont want to change the fragment inside the viewpager. I want to replace the fragment which includes the viewpager. Check my edit. I added my view hierarchy. – beeb Aug 10 '16 at 23:07
  • I see. That is a bit more complicated. – usajnf Aug 10 '16 at 23:09

2 Answers2

4

I resolved my problem.

Now i always replace the CDMBasicMovieFragment like this:

getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, CDMBasicMovieFragment.newInstance()).commit();

On calling the FragmentPagerAdapter's constructor i changed getFragmentManager to getChildFragmentManager.

I thought i can create all fragment one time and just replace them if i click on the item of the navigationbar (drawner). But the replace will remove the fragment which was created one time and when i want to replace the removed fragment again, it's not there. That's why i got an empty viewpager.

beeb
  • 1,615
  • 1
  • 12
  • 24
1

Try below code snippet, In your custom pager adapter:

@Override
public int getItemPosition(Object object) {
     return POSITION_NONE;
}

Then in your Activity containing the ViewPager:

final MyPagerAdapter adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
     @Override
     public void onPageSelected(int position) {
          ... anything you may need to do to handle pager state ...
          adapter.notifyDataSetChanged(); //this line will force all pages to be loaded fresh when changing between fragments
     }
}