Animation when pressing the back button

I added the following code to my activity and got the desired animation, but clicking on the back button does not match I. The action just closes normally. How to add animation when you click the back button

public void notesAndCodeClick(View v){

    Intent notesIntent = new Intent(MainActivity.this, NotesActivity.class);
    ActivityOptions notesoptions = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
    startActivity(notesIntent, notesoptions.toBundle());
}
+4
source share
4 answers

Try it,

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        MainActivity.this.overridePendingTransition(R.anim.trans_right_in,
                R.anim.trans_right_out);
    }

Add both of the files listed below to your animation folder

res → anim

trans_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="-100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0" />

</set>

trans_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="100%p" />

</set>
+2
source

You can set the IN and OUT animations for the Activity by clicking the back button.

Animation from left to right:

Put this file in res / anim / left_to_right.xml : -

 <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
      <translate android:fromXDelta="-100%" android:toXDelta="0%"
                 android:fromYDelta="0%" android:toYDelta="0%"
                 android:duration="700"/>
    </set>

Right to left animation:

res/anim/right_to_left.xml: -

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

onBackPressed(): -

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);   
}
+1

, , .

finish();
overridePendingTransition(R.anim.nothing,R.anim.nothing);

....

0

"", onBackPressed() Activity.

@Override
public void onBackPressed() {
    super.onBackPressed();
    // add your animation   
}

Android

public void onBackPressed():, . , , , .

0

Source: https://habr.com/ru/post/1613815/


All Articles