Tarun Sharma
Tarun Sharma

Reputation: 599

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

}

Upvotes: 14

Views: 20211

Answers (8)

Giulio
Giulio

Reputation: 1

When you navigate to or from an Activity, pop animations are not applied automatically.

Once the animation is added to the navigation XML, it must be added to the target activity an override of Activity.finish() with applyPopAnimationsToPendingTransition to have the pop exit animation.

 override fun finish() {
    super.finish()
    ActivityNavigator.applyPopAnimationsToPendingTransition(this)
}

Upvotes: 0

Muhammad Bilal ahmad
Muhammad Bilal ahmad

Reputation: 325

I solved it by overriding the back button behavior.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    finish();
    return true;
}

Upvotes: 2

ab15
ab15

Reputation: 373

I had a back/home button on my action bar which was not picking up the slide animation by overriding onBackPressed or finish. So I had to add this snippet from here. If using the same slide_enter and slide_exit as above:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch(id) {
        // back button
        case android.R.id.home:
            finish();
            overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

In case somebody finds it useful.

Upvotes: 0

Rakshith Shetty
Rakshith Shetty

Reputation: 42

For the Activity enter animation use overridePendingTransition(R.anim.slide_out, R.anim.slide_in); in the onCreate(...) function.

For the exit animation place the same call in onPause(...).

Upvotes: 0

Mukesh Rana
Mukesh Rana

Reputation: 4091

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

Upvotes: 31

Tarun Sharma
Tarun Sharma

Reputation: 599

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

Upvotes: 4

Djordje Tankosic
Djordje Tankosic

Reputation: 1985

I've done something similiar and it works fine, you can change animate() with startAnimation(your_xml), you will also have to make activity background transparent:

<item name="android:windowBackground">@android:color/transparent</item>

This activity will slide down, and the MainActivity will be visible at the moment of sliding because of transparent background. EDIT - with toolbar back button:

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mainlinear.animate()
                    .translationY(ScUtils.getScreenHeight(getApplicationContext()))
                    .setDuration(210)
                    .setInterpolator(new AccelerateInterpolator())
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            ThemeActivity.super.onBackPressed();
                            overridePendingTransition(0, 0);
                           }
                          }).start();
                        }
                    });

Upvotes: 0

Remees M Syde
Remees M Syde

Reputation: 2609

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

Upvotes: 5

Related Questions