Search code examples
androidanimationandroid-activityonstart

Animation not working properly in Activity while on start and destroy


i have 5 to 6 activities in my application I have added left to right and right to left animation on activity start and on finish activity but its not working properly when I click for new activity its working fine it start from right to left but when i press back button its also start from right to left. but when I press hardware back button its work from left to right. here is my code right to left animation

leftin.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:duration="500"/>

leftout.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="0"
    android:toXDelta="-100%p"
    android:duration="500"/>

rightin.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:duration="500"/>

rightout.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="0"
    android:toXDelta="100%p"
    android:duration="500"/>

here is code oncreate activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub__category);
    overridePendingTransition(R.anim.left_in, R.anim.left_out);
}

and here is code of my_back button

  btn_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
            startActivity(myint);
            overridePendingTransition(R.anim.right_in, R.anim.right_out);
            finish();
        }
    });

and here is code of my onbackpress override method.

  @Override
public void onBackPressed() {
    super.onBackPressed();

    Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
    startActivity(myint);
    overridePendingTransition(R.anim.right_in, R.anim.right_out);
    finish();
}

here I am facing problem is that when I press hardware back button its work fine but when i press back button of my UI it not start from left to right but start new activity form right to left


Solution

  • Use this code:

    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    

    Slide in right xml:

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

    Slide out left xml:

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