Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment Transactions with transition - Unique transitionNames are required

Tags:

I want to go from a list view to the detail view and therefore, I use following OnClickListener in my list:

@Override public void onClick(View view) {     Bet bet = (Bet)view.getTag();     FragmentManager fm = getActivity().getSupportFragmentManager();     BetDetailFragment f = BetDetailFragment.create(bet);     String tag = f.getClass().getName();      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)     {         setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));         f.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));     }      FragmentTransaction ft = fm.beginTransaction()             .replace(R.id.frame_container, f, tag)             .addToBackStack(tag);      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)     {         L.d(this, "TRANS: " + TransitionUtil.getTransitionNameBetLogo1(bet) + "|" + view.findViewById(R.id.ivLogo1));         L.d(this, "TRANS: " + TransitionUtil.getTransitionNameBetLogo2(bet) + "|" + view.findViewById(R.id.ivLogo2));         ft.addSharedElement(view.findViewById(R.id.ivLogo1), "1");//TransitionUtil.getTransitionNameBetLogo1(bet));         ft.addSharedElement(view.findViewById(R.id.ivLogo2), "2");//TransitionUtil.getTransitionNameBetLogo2(bet));     }     ft.commit(); } 

My functions return unique names, I have two different views, but still it does not work. I already commented unnecessary functions out and wrote some unique transaction names in there by hand... But still, I get this exception, in the line of the first addSharedElement:

java.lang.IllegalArgumentException: Unique transitionNames are required for all sharedElements         at android.support.v4.app.BackStackRecord.addSharedElement 

EDIT

Without the shared elements, everything is working perfectly fine...

like image 489
prom85 Avatar asked Jun 15 '15 14:06

prom85


People also ask

What is fragment transition?

The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation. One of these is the Animation Framework, which uses both Animation and Animator . The other is the Transition Framework, which includes shared element transitions.

What is the use of FragmentManager in Android?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.

How do I switch fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


2 Answers

The problem is, that addSharedElement does NOT set the transaction name of the view!

So in my example I would have to set it with following code:

ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo1), "1"); ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo2), "2"); 

BEFORE I add this views to the FragmentTransaction...

Afterwards following works just fine and as expected:

ft.addSharedElement(view.findViewById(R.id.ivLogo1), "1"); ft.addSharedElement(view.findViewById(R.id.ivLogo2), "2"); 
like image 70
prom85 Avatar answered Oct 31 '22 12:10

prom85


You must set the same transitionName in the xml layout element of each fragment. For example:

Fragment A:

<TextView  android:id="@+id/my_text_view"  ...  android:transitionName="transtion_name_example"/> 

Fragment B:

<TextView  android:id="@+id/my_text_view"  ...  android:transitionName="transtion_name_example"/> 

And the code will be something like this:

yourTransaction.addSharedElement(view, view.transactionName) 
like image 27
ycesar Avatar answered Oct 31 '22 10:10

ycesar