Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slide out animation not working on back press button?

I am using animations when entering or exiting the activity,entering to an activity animation works fine but exit animation does not work well. I start exit animation when i press back button.What happens is it first start enter animation for current activity then show the last activity what i want a simple exit animation on back button press.

Slide_out.xml

    <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p" >

</translate>

Slide_in.xml

    <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="100%p"
    android:toXDelta="0%p" >

</translate>

On Action bar back button pressed

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.home:

        finish();
        overridePendingTransition(R.anim.slide_out, R.anim.slide_in);
        return true;


    default:
        break;
    }

    return super.onOptionsItemSelected(item);

}
like image 618
Tarun Sharma Avatar asked Feb 14 '15 05:02

Tarun Sharma


4 Answers

Firstly create one more animation i.e nothing.xml in your anim folder

nothing.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="0%"
    android:toXDelta="0%" >

</translate>

here is your slide_in.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="-100%"
    android:toXDelta="0%" >

</translate>

and slide_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="0%"
    android:toXDelta="100%" >

</translate>

Now call your NewActivity like this

startActivity(new Intent(CurrentActivity.this, NewActivity.class));
overridePendingTransition(R.anim.slide_in, R.anim.nothing);

and then on your back button click do this

finish();
overridePendingTransition(R.anim.nothing, R.anim.slide_out);
like image 150
Mukesh Rana Avatar answered Oct 12 '22 23:10

Mukesh Rana


I used slide_in.xml

    <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="100%"
    android:toXDelta="0%" >

</translate>

slide_out.xml

    <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="0%"
    android:toXDelta="-100%" >

</translate>

slide_enter.xml

  <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="-100%"
    android:toXDelta="0%" >

</translate>

slide_exit.xml

    <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="0%"
    android:toXDelta="100%" >

</translate>

Code

        Intent i=new Intent(Authentication.this,Login.class);
        startActivity(i);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

To previous

         finish();
         overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
like image 43
Tarun Sharma Avatar answered Oct 12 '22 23:10

Tarun Sharma


Add animation in onBackPressed, it will show the animation while clicking the back button.

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);   
}
like image 28
Remees M Syde Avatar answered Oct 12 '22 23:10

Remees M Syde


I solved it by overriding the back button behavior.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    finish();
    return true;
}
like image 25
Muhammad Bilal ahmad Avatar answered Oct 13 '22 01:10

Muhammad Bilal ahmad