I am using a device running on Android 5.0, minSdk is 21, and targetSdk is 22.
Application description
I am in an application with two actions, activity A contains a GridView with images, and text and activity B contains one ImageView , one TextView and a fragment. This snippet contains a list of images associated with the text. When you click on the image of activity A, a transition of the common element takes place, which moves the image and text in ImageView and TextView activity B. I also set the fade transition between activity A and B. The fragment is dynamically added to activity B using the onCreate() method. I install and enter and complete the 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();
I used a trick about the exit of the fragment transition, when I press 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 application is a child of Theme.Material , so I haven't defined
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS); getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
and
<item name="android:windowActivityTransitions">true</item> <item name="android:windowContentTransitions">true</item>
(I tried, and it didn't matter).
Problem with application
My problem is that the transition to the input of the fragment does not work properly, while the transition to the output works fine. I tried different cases:
- there is no clearly defined transition of activity: there is no transition to input, a fragment already exists when there is a transition to a common element.
- determine the transition of activity from java to actions A and B:
getWindow().setEnterTransition(new Fade()); getWindow().setExitTransition(new Fade()); getWindow().setEnterTransition(new Fade()); getWindow().setExitTransition(new Fade()); The result is the same as the absence of an explicit transition of activity. - define activity transition with XML:
<item name="android:windowEnterTransition">@transition/fade</item> <item name="android:windowExitTransition">@transition/fade</item> As a result, the fragment is entered by attenuation, and Fragment.setEnterTransition() ignored. - define fragment transition in XML:
<item name="android:fragmentEnterTransition">@transition/fade</item> result is that it is completely ignored, all cases will coincide before that.
I tried other things to make it work like this: myFragment.setAllowEnterTransitionOverlap(false); but nothing ... I also tried installing the click listener on ImageView and adding the fragment only when I click on the image, and it will work! From this, I thought that there might be a problem in the transition time of the fragment compared to the activity life cycle, so I tried to put the code in the onResume() method, but still nothing.
Then I tried using the 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 works!
Finally, I tried adding an empty fragment, and then replacing it with 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 am wondering if there is something that I missed to make it work without using the trick ... And the fact is that I know that it can work, because I started working on the application, maybe , 5 months ago, and it was at work !! Therefore, I do not know what happened, I did not update the version for Android.
Bonus error : it is still concerned with the transition of the fragment. Before using the application as described above, I tried the version without going over a common element. And if I donβt specify the transition name on the TextView Activity B, the fragment will never have an input transition. But, as before, if it is installed in the onclicklistener method, it will work. I tried this on two devices running Android 5.0.x and the transition did not work. I tried the device with Android 5.1.1, and the transition worked (without changing anything in the code)! Still don't know what is going on ...
So my last question is: what explains this behavior and is there a way to make it work without using a trick?