user7824858
user7824858

Reputation: 69

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

Upvotes: 1

Views: 2113

Answers (4)

user7824858
user7824858

Reputation: 69

hi here is how i achieved this animation. i know my thinking of achieving this is not professional way but finally i achieved it by my way. here is i have activity A,B,C so on activity B i check that is it clicked from Activity A or C and according to that i used if condition and implement animation some way like below code

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = pref.edit();

    String fromhome =pref.getString("from_home_list_adapter","");
    if(fromhome.equals("yes"))
    {
        editor.putString("from_home_list_adapter","no");
        editor.commit();
        overridePendingTransition(R.anim.left_in, R.anim.left_out);
    }

again i say this is not professional way to handle animation but its works

Upvotes: 0

Simranjeet Singh
Simranjeet Singh

Reputation: 173

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>

Upvotes: 1

Aldrin Joe Mathew
Aldrin Joe Mathew

Reputation: 482

I don't think you need to invoke overridependingtransitions() method in onCreate().

Upvotes: 0

Mehul Gajjar
Mehul Gajjar

Reputation: 431

write overridePendingTransition(R.anim.right_in, R.anim.right_out); after finish(); method.

Upvotes: 0

Related Questions