Home » Android » android – Custom up navigation animation

android – Custom up navigation animation

Posted by: admin June 15, 2020 Leave a comment

Questions:

When back button was clicked:

@Override
public void onBackPressed() {
    finish(); //go back to the previous Activity
    overridePendingTransition(R.anim.slide_in_exit, R.anim.slide_out_exit); 
}

This will animate the views. However how to do that when up navigation button in action bar was clicked?

How to&Answers:

Put overridePendingTransition in onCreate instead to make the transition happen whenever you leave the activity.

Answer:

cYrixmorten’s answer doesn’t work well when I want to add some animations to up navigation button, So I override the onOptionsItemSelected method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    switch(itemId){
        case android.R.id.home:
            super.onOptionsItemSelected(item);
            this.finish();
            overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            break;
        default:
            break;
    }

    return true;
}