Search code examples
androidandroid-5.0-lollipopactivity-transition

Android Lollipop home button return animation


I'm trying the new Android API, specifically the new animations. I have two activities and used setEnterTransition() and setExitTransition() on the second activity with a Slide transition. Everything works fine when I switch activities using the buttons inside them, or using the back button, but when I'm on the second activity and I press the Home Button the return animation is not played...

Second activity onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getActionBar() != null) {
        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    Slide slideTransition = new Slide();
    slideTransition.setDuration(1000);
    slideTransition.setSlideEdge(Gravity.RIGHT);

    getWindow().setEnterTransition(getSlideTransition(Gravity.RIGHT, 1000));
    getWindow().setExitTransition(getSlideTransition(Gravity.LEFT, 1000));

    getWindow().setAllowEnterTransitionOverlap(true);

    setContentView(R.layout.activity_second);

    //...
}

The first activity is set as parent of the second activity in the manifest.

I find some solutions that use the overridePendingTransition() method, but I would like a solution that uses the new methods (if that is possible).

Thanks!


Solution

  • This is by design. The return transition is only triggered when the activity is explicitly finished (i.e. you press the back button or call finishAfterTransition()). When you press the home button, you are putting the application into the background so that the user can return to that same activity at a later time. If you were to finish the activity when you pressed the home button, the user would be confused as to why they were not taken to that same activity when they return to the application later on.

    It is also worth mentioning that the new Lollipop transition APIs are not meant to replace overridePendingTransition(). The two are fundamentally different. The Lollipop transition APIs give you a way to animate the contents inside an activity's view hierarchy individually when you switch from one activity to another. On the other hand, overridePendingTranition() allows you to override the system's default window animation when the activity window is being added or removed from the screen. In other words, the former operates on the views inside the activity's window whereas the latter operates on the entire activity window itself.

    One last major difference between the two is that the new Lollipop transition APIs only work between two activities that belong to the same task. If you want to perform an exit/enter animation when navigating between two activities belonging to two different tasks, you'd need to use overridePendingTransition() instead.