5

PlayPanel activity should close in the form of a sliding panel to MainActivity when back button is pressed. However, there is no animation. PlayPanel activity simply closes normally.

PlayPanel activity

public class PlayPanel extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_panel);
    }

    @Override
    public void onBackPressed() {

        Intent intent = new Intent(
             PlayPanel.this,
             MainActivity.class
             );

        startActivity(intent);

        overridePendingTransition(
             0, 
             R.anim.play_panel_close_background
             );
    }

    // onCreateOptionsMenu goes here

    // onOptionsItemSelected goes here
}

play_panel_close_background.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="600"
        />
</set>

the solution:

 @Override
    public void onBackPressed() {
        super.onBackPressed();
        //startActivity(new Intent(this, MainActivity.class));
        overridePendingTransition(
                0,
                R.anim.play_panel_close_outgoing_activity
        );
    }
the_prole
  • 8,275
  • 16
  • 78
  • 163

5 Answers5

14

I implemented same animation before. This code works. Please try it. Thanks.

DetailActivity

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        overridePendingTransition(R.anim.activity_slide_start_enter, R.anim.activity_scale_start_exit);
    }

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

    @Override
    public void onBackPressed() {
        finish();
    }
}

activity_slide_start_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">
    <translate
        android:duration="200"
        android:fromXDelta="100%"
        android:fromYDelta="0%"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

activity_scale_start_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">

    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.9"
        android:toYScale="0.9" />

</set>

activity_scale_finish_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">

    <scale
        android:duration="200"
        android:fromXScale="0.9"
        android:fromYScale="0.9"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

activity_slide_finish_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">
    <translate
        android:duration="200"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />
</set>
Konifar
  • 1,949
  • 1
  • 12
  • 7
6

According to documentation, the first argument to overridePendingTransition is the animation used for the incoming activity, ie. your MainActivity. And the second argument is used for the outgoing activity, ie. your PlayPanel.

play_panel_close_background.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:zAdjustment="top">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="600"
        />
</set>

@Override
public void onBackPressed() {
    //super.onBackPressed();
    startActivity(new Intent(this, MainActivity.class));
    overridePendingTransition(
            0,
            R.anim.play_panel_close_background
    );
}
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • Are you sure this solution works? I set the first argument to `overridePendingTransition` to zero, and set the other parameters, but the activity doesn't not slide. It still just disappears. By the way, `overridePendingTransition` does not work with `super.onBackPressed()` it only works with `finish()` and `startActivity()` – the_prole Oct 05 '15 at 01:44
  • Do you still have a problem? – tachyonflux Oct 05 '15 at 02:02
  • 1
    Yes, I have the same problem. [This](http://stackoverflow.com/questions/9294446/make-new-activity-appear-behind-old-one-during-transition) is exactly my problem, but I cannot make the solution `android:zAdjustment=""` work. Can you make the solution with this attribute? – the_prole Oct 05 '15 at 02:40
  • 1
    yeah, if you're using `startActivity`, then you need to add `android:zAdjustment="top"` to the exit transition so that it is on top of the new activity. With `super.onBackPressed()` or `finish()`, you don't need to, since there is no new activity. – tachyonflux Oct 05 '15 at 03:20
  • Okay, this was the solution the entire time. Thanks! – the_prole Oct 05 '15 at 03:23
  • How can I revert back the main activity without starting a new activity? – the_prole Oct 05 '15 at 03:25
  • Your solution works perfectly WITHOUT `android:zAdjustment="top"` – the_prole Oct 05 '15 at 03:33
  • Okay, I posted the solution at the bottom of my question thread. If you copy and paste it as your answer, I will check mark your solution. – the_prole Oct 05 '15 at 03:36
4

This solution worked for me

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
1

My problem was that I cannot use "override pending transition" with "on back pressed"

 @Override
    public void onBackPressed() {

        //overridePendingTransition(R.anim.play_panel_close_foreground, R.anim.play_panel_close_background);
        //PlayPanel.super.onBackPressed();

        Intent intent = new Intent(PlayPanel.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(0, R.anim.play_panel_close_background);

    }
the_prole
  • 8,275
  • 16
  • 78
  • 163
0

Best Solution I have ever tried,

@Override
public void onBackPressed() {
    finish();
    overridePendingTransition(R.anim.anim_right_to_left_enter, R.anim.anim_right_to_left_exit);
}
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37