lcw_gg
lcw_gg

Reputation: 669

Problems with Fragment enter transition

I'm using a device running on Android 5.0, the minSdk is 21 and the targetSdk is 22.

App description

I'm on an app with 2 activities, activity A contains a GridView with pictures and text and activity B contains one ImageView, one TextView and a fragment. This fragment contains a list of images associated to a text. When clicking on an image of activity A, there is a shared element transition which moves the image and the text to the ImageView and the TextView of activity B. I also set a fade transition between activity A and B. The fragment is added dynamically to activity B in the onCreate() method. I set and enter and exit transition to the fragment:

 MyFragment myFragment = new MyFragment();
 myFragment.setEnterTransition(new Slide());
 myFragment.setExitTransition(new Slide);
 getFragmentManager().beginTransaction()
                .add(R.id.container, myFragment, MyFragment.TAG)
                .commit();

About the fragment exit transition, I used a trick, when pressing the back button, I replace the fragment with another one to force it:

@Override
public void onBackPressed() {
    if (myFragment.isVisible()) {
        getFragmentManager().beginTransaction()
                .replace(R.id.container, new Fragment())
                .commit();
    }
    super.onBackPressed();
}

The theme of my app is a child of Theme.Material so I didn't define

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

nor

<item name="android:windowActivityTransitions">true</item>
<item name="android:windowContentTransitions">true</item>

(I tried with and it made no difference).

App problem

My issue concerns the fragment enter transition which is not working properly, while the exit transition works fine. I tried different cases:

I tried other things to make it work like this: myFragment.setAllowEnterTransitionOverlap(false); but nothing... I also tried to set a click listener on the ImageView and add the fragment only on clicking the image and it worked ! From this, I thought that there might be a problem in the timing of the fragment transition compared to the activity lifecycle, so I tried putting the code in the onResume() method but still nothing.

Then I tried to use a postDelayed() method:

MyFragment myFragment = new MyFragment();
myFragment.setEnterTransition(new Slide());
myFragment.setExitTransition(new Slide);
mImageView.postDelayed(new Runnable() {
            @Override
            public void run() {
         getFragmentManager().beginTransaction()
            .add(R.id.container, myFragment, MyFragment.TAG)
            .commit();
            }
        }, 1000);

and it also worked!

At last I tried to add an empty fragment and then replace it by myFragment:

MyFragment myFragment = new MyFragment();
myFragment.setEnterTransition(new Slide());
myFragment.setExitTransition(new Slide);
getFragmentManager().beginTransaction()
                .add(R.id.container, new Fragment)
                .commit();
getFragmentManager().beginTransaction()
                .replace(R.id.container, myFragment, MyFragment.TAG)
                .commit();

But still nothing...

So now I'm wondering if there is something I missing to make it work without using a trick... And the thing is I know it can work because I started to work on the app maybe 5 months ago and it was working!! So I don't know what happened, I didn't update my Android version.

Bonus bug: It is still about the fragment enter transition. Before having the app like I described before, I tried a version without shared element transition. And if I don't set a transition name to the TextView of Activity B, the fragment will never have an enter transition. But same as before, if set in a onclicklistener method, it will work. I tried this on 2 devices running on Android 5.0.x and the transition was not working. I tried on a device with Android 5.1.1 and the transition worked (without changing anything in the code)! Still no idea on what is happening...

So my final question is: What is the explanation of this behavior and is there a way of making it work without using a trick?

Upvotes: 9

Views: 3354

Answers (3)

lilienberg
lilienberg

Reputation: 1272

I was working defining fragment transitions this way some years ago, now i faced this issue as well. I looked at my old code and found the answer there. In my case the layout had several ViewGroups and i wanted to have different enterTransitions for the children. For me the issue got solved by adding android:transitionGroup="false" to my root container of the layout file.

Upvotes: 0

ElDuderino
ElDuderino

Reputation: 3263

Use setCustomAnimations from the FragmentTransaction class.

getFragmentManager().beginTransaction() .setCustomAnimations(R.id.inAnim, R.id.outAnim,R.id.inAnim, R.id.outAnim) .replace(R.id.container, myFragment, MyFragment.TAG) .commit();

This way you can define an animation per transition.

Upvotes: 0

android.rookie
android.rookie

Reputation: 161

Try adding the enter and exit transitions for the fragment in its OnCreate instead. That seemed to have worked for me

Upvotes: 2

Related Questions