Shared element when using startActivityForResult
<p>I have an app with two <code>activities</code>, where the second functions as an image "picker" and delivers some information to the first, so that an <code>ImageView</code> can be updated accordingly. </p>

<p>For this, I'm calling the second <code>activity</code> through <code>startActivityForResult()</code>, and updating the <code>ImageView</code> according to information received on <code>onActivityResult()</code>. </p>

<p>The problem arises when I try to animate a shared element between the picker and the main <code>Activity</code>: it seems like the transition "snapshots" the first <code>Activity</code>, and even if the <code>ImageView</code> is updated as soon as the <code>Activity</code> resumes, the transition animation will "flicker" an older version of the <code>View</code> before showing the current version.</p>

<p>Here's an example app I made to illustrate what I'm talking about:</p>

<p><a href="https://i.stack.imgur.com/Uf6Ym.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uf6Ym.gif" alt="Visual description of the bug"></a></p>

<p><strong>What would be the best way to implement this animation seamlessly? Should another approach be used?</strong></p>

<p>Here's the code used for the example:</p>

<p>Main Activity</p>

<pre><code>public class MainActivity extends AppCompatActivity {

    ImageView mainImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setTitle("Main Activity");

        mainImageView = (ImageView) findViewById(R.id.imageView);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PickerActivity.class);
                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation(MainActivity.this, mainImageView, "shape_transition");
                startActivityForResult(intent, 1, options.toBundle());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        int shapeNumber = intent.getIntExtra("shape_number", -1);

        switch (shapeNumber) {
            case 1:
                mainImageView.setImageResource(R.drawable.blue);
                break;
            case 2:
                mainImageView.setImageResource(R.drawable.green);
                break;
            default:
                //something else
                break;
        }


    }
}
</code></pre>

<p>Picker Activity:</p>

<pre><code>public class PickerActivity extends AppCompatActivity {

    ImageView blueImageView;
    ImageView greenImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker);
        getSupportActionBar().setTitle("Picker Activity");

        blueImageView = (ImageView) findViewById(R.id.imageView2);

        blueImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                blueImageView.setTransitionName("shape_transition");
                Intent intent = new Intent(PickerActivity.this, MainActivity.class);
                intent.putExtra("shape_number", 1);
                setResult(RESULT_OK, intent);
                finishAfterTransition();
            }
        });

        greenImageView = (ImageView) findViewById(R.id.imageView3);

        greenImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                greenImageView.setTransitionName("shape_transition");
                Intent intent = new Intent(PickerActivity.this, MainActivity.class);
                intent.putExtra("shape_number", 2);
                setResult(RESULT_OK, intent);
                finishAfterTransition();
            }
        });
    }
}
</code></pre>

<p>Main Activity xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="demonstration.sharedelementproblemexample.MainActivity"&gt;

    &lt;ImageView
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:id="@+id/imageView"
        android:transitionName="shape_transition"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/red" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick"
        android:id="@+id/button"
        android:layout_below="@+id/imageView"
        android:layout_alignParentStart="true" /&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>Picker Activity xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="demonstration.sharedelementproblemexample.PickerActivity"&gt;

    &lt;ImageView
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:id="@+id/imageView2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/blue" /&gt;

    &lt;ImageView
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:id="@+id/imageView3"
        android:src="@drawable/green"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/imageView2" /&gt;
&lt;/RelativeLayout&gt;
</code></pre>