Search code examples
androidanimationandroid-animationandroid-transitionsshared-element-transition

Back shared element transition between activities


I use native Android Transition API to animate transition between activities. Here's the source I use to launch activity:

        Intent intent = new Intent(MainActivity.this, DetailActivity.class);
        Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, imageView, imageView.getTransitionName()).toBundle();

        MainActivity.this.startActivity(intent, bundle);

When I tap on hardware back button it returns to previous activity with expected reversed transition animation, but when I tap on "Up" button in Toolbar it returns to previous activity with default animation:


Solution

  • Add following code to the activity, that is being finished:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                supportFinishAfterTransition();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    Source