11

I'm just starting out with Android and I followed this guide on adding the Up navigation to an Activity on the Action Bar.

http://developer.android.com/training/implementing-navigation/ancestral.html

However, the transition of when I use the up button from the Action Bar makes it show as the fade-in transition of opening a new activity rather than the fade-out that it should be, as it does when pressing the back button on the bottom.

What can I do to make it display the correct transition? Thanks.

Aaron
  • 7,000
  • 2
  • 21
  • 37
Linkandzelda
  • 611
  • 14
  • 24

7 Answers7

17

Declare android:launchMode="singleTop" inside your Manifest.xml for your parent Activity.

The transition will then change to the default fade out when calling something like:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

(from: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp)

ILadis
  • 261
  • 3
  • 8
  • 1
    When there was a custom transition between parent and current activity, including a shared element, the only way I can get it to reverse that transition is by calling `finishAfterTransition` directly. No calls to `NavUtils` methods will do the job, even if I overrode `finish` in the current activity to call `finishAfterTransition`. Does anyone know why? I'm about to search for/post my own question on this topic. – androidguy Aug 24 '17 at 03:04
11

You should use navigateUpTo() method and set the flag no animation to the intent, like this:

final Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
NavUtils.navigateUpTo(this, intent);

You'll have the 'nice' back animation, and the correct use of the Up button. this is the current activity.

Ciprian
  • 2,879
  • 3
  • 28
  • 28
  • I think this is not working i tried replacing - NavUtils.navigateUpFromSameTask(this); with your code still its standard fade away – ShankPossible Aug 19 '18 at 18:24
1

@Ciprian's solution works for me.

Also, navigateUpFromSameTask() is just a convenience method equivalent to calling:

final Intent intent = NavUtils.getParentActivityIntent(this);
NavUtils.navigateUpTo(this, intent);

When you add the Intent.FLAG_ACTIVITY_NO_ANIMATION flag, the defaut activity fade out transation takes effect.

trojantale
  • 196
  • 1
  • 7
  • i have used @android:transition/slide_left for exit from my parent activity , hence when i click back button slide_right is called by default however in case of NavUtils its calling fade i have removed //NavUtils.navigateUpFromSameTask(this); – ShankPossible Aug 19 '18 at 18:30
1

I struggled a lot for this same problems and managed to solved it as follows:

       case android.R.id.home:
            onBackPressed();
            return true;

Whenever your pressed the back button onBackPressed() is called too. Calling it when the action bar back button is pressed will merely do the same thing.

edmond
  • 833
  • 3
  • 13
  • 31
  • make sense and worked , just one question this will come at a cost that we will not be able to use launchmodes right , or am i missing something – ShankPossible Aug 19 '18 at 18:34
0

I changed it to finish(); and this seems to do what I want.

Linkandzelda
  • 611
  • 14
  • 24
  • 5
    yes, but that's not correct, check out this presentation here why it is not recommended to do that: https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android – Ciprian Dec 12 '13 at 13:53
0

It might get confusing like it was for me. However, my solution was simply adding android:launchMode="singleTop" to all of the parents regardless of the parent having another parent.

Hope it helps :)

gmartinsnull
  • 1,075
  • 1
  • 12
  • 11
-1

you can use the method overridePendingTransition():

Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.

http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)

Cliffus
  • 1,481
  • 2
  • 11
  • 7
  • this does not work for NavUtils navigateUpFromSameTask() / navigateUpTo() – SjoerdvGestel May 10 '16 at 11:31
  • checking the source code, it should work ;-) http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/app/NavUtils.java – Cliffus May 12 '16 at 09:53