The new Shared Element Transitions works when i use Fragment 'replace' but i can't seem to make it work fragment 'add'. I use the same container in both the cases.
More details:
Activity - layout->
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ffff" android:orientation="vertical" > </FrameLayout>
On launch of the Activity, I add Fragment1 to the screen
getFragmentManager().beginTransaction().replace(R.id.container,new TransitionTestFragment1(), "TransitionTestFragment1").commit();
On a click event for a view in the layout of Fragment1 -> I add Fragment2 to the screen. I set the listener in the first Fragment's onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final TransitionSet transitionSet = new TransitionSet(); transitionSet.addTransition(new ChangeImageTransform()); transitionSet.addTransition(new ChangeBounds()); transitionSet.addTransition(new ChangeTransform()); transitionSet.setDuration(300); View v=inflater.inflate(R.layout.fragment1, null); final View image=v.findViewById(R.id.image); image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setSharedElementReturnTransition(transitionSet); Fragment fragment = new TransitionTestFragment2(); fragment.setSharedElementEnterTransition(transitionSet); FragmentTransaction ft = getFragmentManager().beginTransaction() .replace(R.id.container, fragment) .addToBackStack("transaction") .addSharedElement(image, "MyTransition"); ft.commit(); } }); return v;
}
I have this image view in the layouts of both fragments
<ImageView android:layout_width="300dp" android:layout_height="300dp" android:src="@drawable/ic_launcher" android:transitionName="MyTransition" />
Now, the transition does not work if i use FragmentTransaction.add()
to add the second fragment, but it works if i use FragmentTransaction.replace()
instead. How can i make it work with add()? Is it possible at all?
Shared Element Transition is one of the most seen animations in Android apps. This type of animation is used when we have to open an item from a ListView or RecyclerView. Shared Element Transition in Android determines how shared element views are animated from activity to activity or fragment to fragment.
If you are running into issues with shared element transitions such as unreliable transitions, the problem likely lies with the order of operation or race conditions associated with loading asynchronous data.
And definitely no “shared element transitions” Need front-end development training? Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.
As a FragmentTransaction is treated as a single atomic set of operations, calls to both detach and attach on the same fragment instance in the same transaction effectively cancel each other out, thus avoiding the destruction and immediate recreation of the fragment's UI.
I know this is an old question but recently i had the same problem: .replace was not an option.
I had to use .Hide and .Add and what worked for me was to set:
setReorderingAllowed(true)
on the transaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With