Question

I'm trying to make a fragment to display tips for the user. The fragment's createView method returns a ViewPager and setup the PagerAdapter through AsyncTask. This fragment is added dinamically in the FragmentActivity when the user press a button. If the user reaches the last item of press the same button again, this fragment is removed from the FragmentActivity. It's important to note that I add the fragment to the FrameLayout idenfified by android.R.id.content.

Ok, the first time I press the button, it works as expected. But the second time I press the button, the ViewPager doesn't appear. When I use the View Hierarchy to see what's happenning, I see the ViewPager in the right place, but with no items. I debugged and noted that the getCount() of the PagerAdapter is called, but the getItem is never called.

This is the createView method of my fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View tipsView = inflater.inflate(R.layout.tip_manager_layout, container, false);
  this.tipPagerAdapter = new TipPagerAdapter(getFragmentManager(), getArguments().getIntArray(TIP_LAYOUT_IDS_KEY));
  this.viewPager = (ViewPager) tipsView.findViewById(R.id.tip_pager);
  this.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
    @Override
    public void onPageSelected(int position) {
      if(position == (tipPagerAdapter.getCount()-1)){
         stop();
      }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
  });
  // Workaround to set the PagerAdapter within a fragment transaction
  new SetViewPagerAdapterTask().execute();
  return tipsView;
}

I add the fragment this way:

FragmentTransaction fragmentTransaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(android.R.id.content, this, TAG);
fragmentTransaction.commit();

And remove the fragment this way:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(tipManagerFragment);
fragmentTransaction.commit();

This is the layout inflated by the fragment:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tip_pager"
    android:layout_width="fill_parent"
    android:layout_height="175dp"
    android:layout_gravity="center_horizontal|bottom"
    android:paddingBottom="2dp" />

Could someone help me with this problem?

EDIT:

For a while, I'm using the show and hide methods of the fragment after adding it to the activity. But it's not the correct approach. I can't understand why I can't add again a fragment with a ViewPager. The ViewPager is added and removed correctly, but the second time, it simply doesn't show anything.

Was it helpful?

Solution

After a long time thinking about my problem, I had the following idea: maybe the problem is related to the fact that I'm removing the fragment that has the ViewPager, but I'm not removing the fragments used by the ViewPager. Then, these fragments continues in the FragmentManager and something strange happens when the new ViewPager tries to use them.

So, I started trying to remove all the fragments. To do this, I created a method in my FragmentPagerAdapter this way:

public void destroy(FragmentTransaction fragmentTransaction){
    for(Fragment tipFragment : tipFragments){
        fragmentTransaction.remove(tipFragment);
    }
}

The adapter is using the tipFragments to create its content. So, I get a fragmentTransaction as parameter and I remove all these fragments. When I want to remove my fragment, I use this code:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
tipPagerAdapter.destroy(fragmentTransaction);
fragmentTransaction.remove(TipManagerFragment.this);
fragmentTransaction.commit();

This way, I remove all the fragments and when I add the fragment which has the ViewPager again, everything works well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top