Search code examples
androidanimationandroid-activityandroid-animation

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);

}

Solution

  • 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);