Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shared element transition works with FragmentTransaction.replace() but doesn't work with FragmentTransaction.add()

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?

like image 906
Vinay W Avatar asked Mar 19 '15 12:03

Vinay W


People also ask

What is shared element transition in Android?

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.

Why are my shared element transitions not working?

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.

Are there any shared element transitions in front-end development?

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.

How is a fragmenttransaction treated as an atom?

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.


1 Answers

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.

like image 65
GiampaoloGabba Avatar answered Sep 22 '22 23:09

GiampaoloGabba