title: Shared element back transition not working with recyclerview and cardviews in fragments
body: <p>I'm trying to create a transition between a recycler view with cardviews fragment to a fragment that only contains 1 card. The problem is that the back transition is not working, while the enter transition is. If I remove <code>setReorderingAllowed(true);</code> then the back transition is working, but the enter transition stops working.
  This is what I have.</p>
  <p>Fragment with recyclerview with cardviews</p>
  <pre><code>public class OrdersFragment extends Fragment implements OrderAdapter.OnOrderListener {
  
      private OrderAdapter mAdapter;
  
      private TextView bonnenTextView;
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          postponeEnterTransition();
      }
  
      public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_bonnen, container, false);
          bonnenTextView = view.findViewById(R.id.bonnen_text_view);
          RecyclerView orderRecyclerView = view.findViewById(R.id.order_recycler_view);
          RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
          orderRecyclerView.setLayoutManager(layoutManager);
          if (mAdapter == null) {
              mAdapter = new OrderAdapter(this);
              fetchOrders();
          }
          orderRecyclerView.setAdapter(mAdapter);
          return view;
      }
  
      private void fetchOrders() {
          new OrderFetcher().fetch(new Callback() {
              @Override
              public void onComplete(Result result) {
                  if (result instanceof Result.Success) {
                      mAdapter.setOrders((Order[]) ((Result.Success&lt;?&gt;)result).data);
                      mAdapter.notifyDataSetChanged();
                  } else {
                      Toast.makeText(getContext(), &quot;Could not load orders&quot;, Toast.LENGTH_SHORT).show();
                  }
                  startPostponedEnterTransition();
              }
          });
      }
  
      @Override
      public void onOrderClick(int position, View view, Order order) {
          Log.i(&quot;OrderClick&quot;, &quot;Transition name &quot; + view.getTransitionName());
  
          View carrierTextView = view.findViewById(R.id.carrier_text_view);
          View numberTextView = view.findViewById(R.id.id_text_view);
          View pickerTextView = view.findViewById(R.id.picker_text_view);
          View locationTextView = view.findViewById(R.id.location_text_view);
  
          FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
          transaction.addSharedElement(view, view.getTransitionName());
          transaction.addSharedElement(carrierTextView, carrierTextView.getTransitionName());
          transaction.addSharedElement(numberTextView, numberTextView.getTransitionName());
          transaction.addSharedElement(pickerTextView, pickerTextView.getTransitionName());
          transaction.addSharedElement(locationTextView, locationTextView.getTransitionName());
          transaction.addSharedElement(bonnenTextView, bonnenTextView.getTransitionName());
          transaction.replace(R.id.nav_host_fragment, BonFragment.newInstance(view.getTransitionName(), order));
          transaction.addToBackStack(null);
          transaction.setReorderingAllowed(true);
          transaction.commit();
      }
  }
  </code></pre>
  <p>xml that goes with fragment above</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.ui.orders.OrdersFragment&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/bonnen_text_view&quot;
          android:transitionName=&quot;bonnen_text_view&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;10dp&quot;
          android:layout_marginTop=&quot;10dp&quot;
          android:text=&quot;@string/orders&quot;
          android:textColor=&quot;@color/secondary&quot;
          android:textSize=&quot;40sp&quot;
          android:textStyle=&quot;bold&quot;
          android:typeface=&quot;normal&quot; /&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/order_recycler_view&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_margin=&quot;20dp&quot;/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>Fragment with single cardview</p>
  <pre><code>public static BonFragment newInstance(String cardTransitionName, Order order) {
          BonFragment bonFragment = new BonFragment();
  
          Bundle args = new Bundle();
          args.putParcelable(&quot;orderParcel&quot;, order);
          args.putString(&quot;cardTransitionName&quot;, cardTransitionName);
          bonFragment.setArguments(args);
  
          return bonFragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          final View root = inflater.inflate(R.layout.fragment_bon, container, false);
  
          CardView orderCard = root.findViewById(R.id.order_card);
          TextView carrierTextView = root.findViewById(R.id.carrier_text_view);
          TextView numberTextView = root.findViewById(R.id.id_text_view);
          TextView pickerTextView = root.findViewById(R.id.picker_text_view);
          TextView locationTextView = root.findViewById(R.id.location_text_view);
  
          if (getArguments() != null &amp;&amp; getArguments().getParcelable(&quot;orderParcel&quot;) != null) {
              Order order = getArguments().getParcelable(&quot;orderParcel&quot;);
              orderCard.setTransitionName(getArguments().getString(&quot;cardTransitionName&quot;));
              if (order != null) {
                  carrierTextView.setTransitionName(&quot;carrier&quot; + order.getIndex());
                  carrierTextView.setText(order.getCarrier());
                  numberTextView.setTransitionName(&quot;number&quot; + order.getIndex());
                  numberTextView.setText(String.valueOf(order.getNumber()));
                  pickerTextView.setTransitionName(&quot;picker&quot; + order.getIndex());
                  pickerTextView.setText(order.getPicker());
                  locationTextView.setTransitionName(&quot;location&quot; + order.getIndex());
                  locationTextView.setText(order.getPosition());
  
                  carrierTextView.setText(&quot;Lorem Ipsum&quot;);
                  numberTextView.setText(&quot;Dolor sit amet&quot;);
                  pickerTextView.setText(&quot;consectetur adipiscing elit&quot;);
                  locationTextView.setText(&quot;Mauris semper&quot;);
  
              }
          }
  
          orderCard.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  getParentFragmentManager().popBackStack();
              }
          });
  
          Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.card_transition);
          setSharedElementEnterTransition(transition);
          setSharedElementReturnTransition(transition);
          return root;
      }
  </code></pre>
  <p>xml that goes with fragment above</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.ui.orders.OrdersFragment&quot;&gt;
  
      &lt;TextView
          android:transitionName=&quot;bonnen_text_view&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;10dp&quot;
          android:layout_marginTop=&quot;10dp&quot;
          android:text=&quot;@string/orders&quot;
          android:textColor=&quot;@color/secondary&quot;
          android:textSize=&quot;40sp&quot;
          android:textStyle=&quot;bold&quot;
          android:typeface=&quot;normal&quot; /&gt;
  
      &lt;androidx.cardview.widget.CardView
          android:id=&quot;@+id/order_card&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:cardBackgroundColor=&quot;@color/primaryLight&quot;
          android:foreground=&quot;?android:attr/selectableItemBackground&quot;
          android:clickable=&quot;true&quot;
          android:layout_margin=&quot;25dp&quot;&gt;
  
          &lt;LinearLayout
              android:transitionName=&quot;order_card_layout&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:orientation=&quot;vertical&quot;
              android:layout_margin=&quot;10dp&quot;&gt;
              &lt;TextView
                  android:id=&quot;@+id/carrier_text_view&quot;
                  android:transitionName=&quot;carrier_text_view&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:textColor=&quot;@color/secondary&quot;
                  android:textSize=&quot;20sp&quot;
                  android:textStyle=&quot;bold&quot;/&gt;
              &lt;TextView
                  android:id=&quot;@+id/id_text_view&quot;
                  android:transitionName=&quot;id_text_view&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:textColor=&quot;@color/secondary&quot; /&gt;
              &lt;TextView
                  android:id=&quot;@+id/picker_text_view&quot;
                  android:transitionName=&quot;picker_text_view&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:textColor=&quot;@color/secondary&quot; /&gt;
              &lt;TextView
                  android:id=&quot;@+id/location_text_view&quot;
                  android:transitionName=&quot;location_text_view&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:textColor=&quot;@color/secondary&quot; /&gt;
          &lt;/LinearLayout&gt;
      &lt;/androidx.cardview.widget.CardView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>This is what it looks like with <code>setReorderingAllowed(true);</code></p>
  <p><a href="https://i.stack.imgur.com/301fI.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/301fI.gif" alt="This is what it looks like with setReorderingAllowed(true);" /></a></p>
  <p>This is what it looks like without <code>setReorderingAllowed(true);</code></p>
  <p><a href="https://i.stack.imgur.com/IaCpi.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IaCpi.gif" alt="This is what it looks like without setReorderingAllowed(true);" /></a></p>
  <p><strong>EDIT</strong>
  After applying the answer given by ianhanniballake I got it working.
  Here is what I did for future reference.
  The only changes I made is in the <code>OrdersFragment</code> class.
  I removed the override of the <code>onCreate()</code> method.
  I removed the <code>startPostponedEnterTransition();</code> from my <code>fetchOrders()</code> method and I added</p>
  <pre><code>@Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          postponeEnterTransition();
          final ViewGroup parentView = (ViewGroup) view.getParent();
          parentView.getViewTreeObserver()
                  .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                      @Override
                      public boolean onPreDraw() {
                          parentView.getViewTreeObserver().removeOnPreDrawListener(this);
                          startPostponedEnterTransition();
                          return true;
                      }
                  });
          super.onViewCreated(view, savedInstanceState);
      }
  </code></pre>
  <p>That's it</p>