onBackPressed() animation issue

I'm trying to override the onBackPressed() method to set my own animation but what's happening is it actually slides from my activity to the same one and then show the new activity.

Here's my code:

@Override
public void onBackPressed()
{
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
}

And here are the animations:

Slide out right:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:duration="400" android:fromXDelta="0%" android:toXDelta="100%" />
<alpha android:duration="400" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

Slide in left:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator= "false" >
<translate android:duration = "400" android:fromXDelta = "-100%" android:toXDelta = "0%" />
<alpha android:duration = "400" android:fromAlpha = "0.0" android:toAlpha = "1.0" />
</set>

Thanks for your help guys.

Upvotes: 0

Views: 766

Answers (4)

Ram Mandal
Ram Mandal

Reputation: 1959

Here is what you can try to solve it:

@Override
public void onBackPressed()
{
    finish();
    overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);


}

Upvotes: 0

piotrek1543
piotrek1543

Reputation: 19351

EDIT: Here is an answer from Android Developers Blog - Back and other hard keys: three stories

If you were to survey the base applications in the Android platform, you would notice a fairly common pattern: add a little bit of magic to intercept the BACK key and do something different. To do this right, the magic needs to look something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

How to intercept the BACK key in an Activity is also one of the common questions we see developers ask, so as of 2.0 we have a new little API to make this more simple and easier to discover and get right:

@Override
public void onBackPressed() {
// do something on back.
return;
}

If this is all you care about doing, and you're not worried about supporting versions of the platform before 2.0, then you can stop here. Otherwise, read on.

So your code should look like this

@Override
public void onBackPressed()
{

   overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
   finish();
}

and

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
            finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

Upvotes: 0

Abhinav singh
Abhinav singh

Reputation: 1456

super.onBackPressed(); delete this line and then work

this is translate slide_in_from_left

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

slide_in_from_right

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

slide_out_to_left

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

slide_out_to_right

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

/

@Override
public void onBackPressed()
{

    overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
   // add which work after you back.
}

and add function which are you work in press back.

Upvotes: 1

Ram Mandal
Ram Mandal

Reputation: 1959

try this once if this works fine to you then you might have some problem with some other section

slide_out_down.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:fromYDelta="0%p"
    android:toYDelta="100%p" />

slide_up_dialog.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:fromYDelta="100%p"
    android:toYDelta="0%p" />

and in your activity add this

@Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.slide_up_dialog, R.anim.slide_out_down);
    }

Upvotes: 0

Related Questions