title: How to restore element in Activity Transition Animation to different position?
body: <p>I have LinearLayout in a vertical ScrollView with multiple ImageViews, when I click on one image view I do a Transition Animation from (Activity 1) to a Full Screen Gallery (Activity 2), the gallery has view pager where user can swipe right / left .. now when user swipes to a different Image in the gallery and presses back the current visible Image in (Activity 2) is restored back (again using transition animation) to the starting position in Activity 1, the problem is that the image is restored until it reaches the old position and then disappears and reveals a different image (since this is already too complicated to discuss) I attached an image.</p>
  
  <p><a href="https://i.stack.imgur.com/vvMpn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vvMpn.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Activity1 Layout:</strong></p>
  
  <pre><code>&lt;LinearLayout&gt;
     &lt;ImageView /&gt;
     &lt;ImageView /&gt;
     &lt;ImageView /&gt;
     &lt;ImageView /&gt;
     &lt;ImageView /&gt;
     &lt;ImageView /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Acivity2 (Gallery) Layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"&gt;
  
      &lt;android.support.v7.widget.AppCompatImageView
          android:id="@+id/animation_image"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/transparent"
          android:scaleType="fitCenter"
          android:src="@drawable/placeholder"
          android:transitionName="@string/transition_article_image_gallery"
          android:layout_centerInParent="true"
          /&gt;
  
      &lt;ViewPager
          android:id="@+id/gallery_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>On click on any image from Activity 1:</p>
  
  <pre><code>public void openGallery(int position, ArrayList&lt;String&gt; images, View view)
      {
          Intent intent = new Intent(getActivity(), GalleryActivity.class);
          intent.putStringArrayListExtra(GalleryActivity.EXTRA_GALLERY_IMAGES, images);
          intent.putExtra(GalleryActivity.EXTRA_POSITION, position);
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
              ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                  getActivity(),
                  new Pair&lt;&gt;(view, TARGET_ELEMENT_TRANSITION_NAME_ATTRIB_VALUE)
              );
              ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
          } else {
              getActivity().startActivity(intent);
          }
      }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  The code from George Mount, worked but there's one trick, it's mainly that this callback is called 2 times when the transition starts and on the way back, so it's important to reset the position and check it on the way back as follows:</p>
  
  <pre><code>if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
          getDetailsActivity().setExitSharedElementCallback(new SharedElementCallback()
          {
              @TargetApi(Build.VERSION_CODES.LOLLIPOP)
              @Override
              public void onMapSharedElements(List&lt;String&gt; names, Map&lt;String, View&gt; sharedElements)
              {
                  if (position != -1) {                        sharedElements.put(getString(R.string.transition_article_image_gallery), articleImagesHolder.getChildAt(position));
                  }
              }
          });
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    public void openGallery(int pos, ArrayList&lt;String&gt; images, View view)
      {
          // reset the return position
          position = -1; 
      }
  </code></pre>
  
  <p>and the following in my fragment:</p>
  
  <pre><code>public void onActivityReenter(int resultCode, Intent data)
  {
      if (resultCode == Activity.RESULT_OK) {
          int pos = data.getIntExtra(EXTRA_IMAGE_POSITION, -1);
          if (pos != -1) {
              position = pos;
          }
      }
  }
  </code></pre>
  
  <p>and the following in my (Activity1):</p>
  
  <pre><code> @Override
  public void onActivityReenter(int resultCode, Intent data)
  {
      ArticleDetailsFragment fragment = (ArticleDetailsFragment) getSupportFragmentManager().findFragmentByTag(ArticleDetailsFragment.class.getSimpleName());
      if (fragment != null) {
          fragment.onActivityReenter(resultCode, data);
      }
  }
  </code></pre>
  
  <p><strong>Update 2</strong>
  It seems that George already wrote some articles about Transitions, but they're not popular nor showing in Google Search Results, so I thought that it'd be useful to share them here:</p>
  
  <p><a href="https://halfthought.wordpress.com/2014/12/" rel="nofollow noreferrer">https://halfthought.wordpress.com/2014/12/</a></p>
  
  <p><a href="https://halfthought.wordpress.com/2014/11/" rel="nofollow noreferrer">https://halfthought.wordpress.com/2014/11/</a></p>
  
  <p><a href="https://halfthought.wordpress.com/2015/06/" rel="nofollow noreferrer">https://halfthought.wordpress.com/2015/06/</a></p>