Search code examples
androidandroid-activityback-buttonandroid-navigation

Android custom transition from child to parent Activity


I have 2 Activities. ActivityA is the parent Activity of ActivityB:

My Manifest file:

<activity
        android:name=".ActivityA"
        android:launchMode="singleTop" />
    <activity
        android:name=".ActivityB"
        android:parentActivityName=".ActivityA"/>

I've changed the transition from ActivityA to ActivityB:

    final Intent intent = new Intent(this, ActivityB.class);
    startActivity(intent);
    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

That works fine. But now I also want to change the transition when going back from ActivityB to ActivityA. In my ActivityB I override the method finish() like this:

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}

The problem is that the finish() method is only called when the hardware back Button is pressed, but not when the "go back arrow" (top left corner of the screen) is pressed.

I also want the custom transition when that Button is pressed.

enter image description here

Any ideas?


Solution

  • you can do it by:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                      finish();
                      overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    
            }
            return super.onOptionsItemSelected(item);
        }