2

I am working on animating an element between two activities using code like this:

Pair<View, String> pair1 = Pair.create(sharedView, transitionName);
Pair<View, String> pair2 = Pair.create(sharedBackgroundView, transitionBackgroundName);

ActivityOptionsCompat transitionActivityOptions =     ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, pair1, pair2);
startActivity(myIntent, transitionActivityOptions.toBundle());

This works great when I`m using phone's back button to get back tot the initial activity, but when I use back button from the Action Bar the transition animation is no longer present. I there any work around for it?

The problem can be seen in the following clip, first time I use phone on screen back button and second time I use Back button form the Action Bar.

https://www.youtube.com/watch?v=Wtdcw2fGBTg

2 Answers 2

4

Here is better solution I'm using.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        finishAfterTransition();
        return true;
    }
    return false;
}

Note: finishAfterTransition() call requires Minimum API Level 21.

2

Intercept the click on home button and call the super onBackPressed()

@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onClickHome();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

protected void onClickHome() {
    super.onBackPressed();
}
2
  • Works :) Thank you so much.
    – Rohit TP
    Commented Jun 29, 2017 at 13:10
  • this is not going to work properly unless the user navigated directly from the desired activity. Whether this works depends on the activity stack! In some cases up navigation may even need to open an intent that isn't even on the stack. This is very much not a dependable solution to this question. Commented Dec 1, 2017 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.