Klaus
Klaus

Reputation: 423

Custom animation on up navigation

When I instanciate a new Activity is use a custom animation like this:

Intent registerIntent = new Intent();
registerIntent.setClassName(getPackageName(), getPackageName() + ".activity.RegisterActivity");
startActivity(registerIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

This way the current/old activity slides out to the left, while the new activity slides in from the right.

When in the new RegisterActivity I use following code to handle the animation when the user uses the "Back" button:

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

Now the current activity leaves to the right and the old activity (from before) comes back in from the left.

I also tried to handle the "Up" Navigation like this, but with no result:

public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }
    return super.onOptionsItemSelected(item);
}

How would i use a custom animation on "Up" navigation? Any suggestions on how to better use animations?

EDIT: slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:duration="500" />
</set>

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="0"
    android:toXDelta="100%p"
    android:duration="500" />
</set>

Upvotes: 0

Views: 964

Answers (2)

Klaus
Klaus

Reputation: 423

I changed the following: Now it works:

Old:

ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue)));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

New:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

Upvotes: 1

Igor Ronner
Igor Ronner

Reputation: 1584

If I understood... I think that this resolve.

Intent registerIntent = new Intent();
registerIntent.setClassName(getPackageName(), getPackageName() + ".activity.RegisterActivity");
   startActivity(registerIntent);
   overridePendingTransition(
            R.anim.animation_in+right_to_left,
            R.anim.animation_out_right_to_left);

public void onBackPressed() {
   super.onBackPressed();
   overridePendingTransition(R.anim.animation_in_left_to_right,
                R.anim.animation_out_left_to_right);
 }

animation_in_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>

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

</set>

animation_out_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>

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

animation_in_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>

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

animation_out_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>

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

you use the same logic to UP navigation

Upvotes: 0

Related Questions