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:
- no explicitly defined activity transition: there is no enter transition, the fragment is already there when the shared element transition is playing.
- define an activity transition with java in activity A and B:
getWindow().setEnterTransition(new Fade()); getWindow().setExitTransition(new Fade());
The result is the same as no explicit activity transition defined. - define an activity transition with XML:
<item name="android:windowEnterTransition">@transition/fade</item> <item name="android:windowExitTransition">@transition/fade</item>
The result is that the fragment will enter by fading and theFragment.setEnterTransition()
is ignored. - define the fragment enter transition in XML:
<item name="android:fragmentEnterTransition">@transition/fade</item>
the result is that it is totally ignored, all the cases before will happen the same.
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?