6

I'm using the ViewPager from the compatibility library, with a FragmentStatePagerAdapter. I have a SherlockFragment which is just a layout with a ViewPager:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        style="@style/TitleStrip" />

</android.support.v4.view.ViewPager>

It has a few children displaying properly most of the time. Though when I want to replace this Fragment using a FragmentTransaction with an animation, its content (the showing child) disappears before the animation takes place. The only thing that remains is the PagerTitleStrip. Here is the code I am using for the replacement:

Fragment fragment = new QueueListFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.fragment_holder, fragment);
ft.addToBackStack(null);
ft.commit();

From what I could check, onDestroyView is called on the Fragment and its child Fragment before the transition begins, which looks odd to me. I would expect it to be called once the view is effectively replaced (after the transition).

The child fragment of the ViewPager isn't at fault as I tried a very simple Fragment displaying a red layout and the problem is the same.

This issue doesn't happen with other kinds of fragments inside the application. The issue doesn't happen either if I use ft.add(R.id.fragment_holder, fragment) instead of ft.replace(R.id.fragment_holder, fragment).

If anyone has a clue about that behavior, it would be greatly appreciated.

nurealam11
  • 537
  • 4
  • 16
GFT
  • 61
  • 3
  • I'm also experiencing this problem, except I use regular fragments, not SherlockFragment, and I use FragmentPagerAdapter, not FragmentStatePagerAdapter. As he said it gets destroyed before the animation is done. Anyone else seen this or have a clue on why this is happening? It seems to be the same issue as this: http://stackoverflow.com/questions/22204258/viewpager-detatch-inner-fragment-before-transaction-animation-is-over – kgrevehagen Apr 14 '14 at 14:46
  • Did you use a child fragment manager? I see this problem only when I use `getChildFragmentManager()` instead of `getFragmentManager()`. Apparently the whole child fragment manager gets destroyed when the parent fragment is about to be replaced. – devconsole Apr 24 '14 at 08:35

0 Answers0