Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override onBackPressed() with overridePendingTransition() animation

Tags:

android

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
        );
    }
like image 752
the_prole Avatar asked Oct 04 '15 23:10

the_prole


2 Answers

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>
like image 55
Konifar Avatar answered Nov 13 '22 17:11

Konifar


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
    );
}
like image 34
tachyonflux Avatar answered Nov 13 '22 16:11

tachyonflux