overridePendingTransition does not work when FLAG_ACTIVITY_REORDER_TO_FRONT is used

35,416

Solution 1

Actually the right solution when using REORDER_TO_FRONT is to call overridePendingTransition in the method onNewIntent() of the activity you are going to open.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

replace with your transitions.

If you need to selectively replace the transition you can add an extra in your intent and check it in the onNewIntent() to decide what to do.

This is not suitable if you don't know the transition that will be opened (if you don't specify it explicitly for example) but otherwise is the right solution.

Solution 2

I ran into this same problem. The trick is to override the transition in the onResume() callback.

@Override
public void onResume() {
    super.onResume();
    overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
}

Solution 3

for me, the solution was to make sure it's ran in the UI thread

runOnUiThread(new Runnable() {
            public void run() {
                startActivity(new Intent(ActivityOne.this, ActivityTwo.class));
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        finish();

Solution 4

I got the same problem. I suggest you check your AndroidManifest.xml to make sure ActivityA and ActivityB both have no set Theme.Translucent.NoTitleBar, this theme contains "android:windowIsTranslucent"=true cause the issue.

Hope this help you.

Solution 5

Calling

overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);

after finish(); of the closing activity worked for me.

finish();
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);

It's better than calling onResume, because it makes the activity more independent about the enter and exit animations:


Calling after finish of sender activity:

Activity A ---Enter Transition 1 (on A)---> Activity B ---Enter Transition 2 (on B)---> Activity C

Activity A <---Exit Transition 1 (on B)--- Activity B <---Exit Transition 2 (on C)--- Activity C

Activity A ---Enter Transition 1 (on A)---> Activity C ---Enter Transition 3 (on C)---> Activity B

Activity A <---Exit Transition 3 (on C)--- Activity C <---Exit Transition 2 (on B)--- Activity B



Calling on onResume of receiver activity:

Activity A ---Enter Transition 1 (on B)---> Activity B ---Enter Transition 2 (on C)---> Activity C

Activity A <---Enter Transition 1 (on A)--- Activity B <---Enter Transition 2 (on B)--- Activity C

Activity A ---Enter Transition 3 (on C)---> Activity C ---Enter Transition 2 (on B)---> Activity B

Activity A <---Enter Transition 1 (on A)--- Activity C <---Enter Transition 3 (on C)--- Activity B

Here the onResume animation always have to be the same no matter which sender activity it is, instead the first approach, where you can custom the animation easily.

Share:
35,416
Admin
Author by

Admin

Updated on August 13, 2020

Comments

  • Admin
    Admin almost 4 years

    I have two activities in the stack, in order to show them I use FLAG_ACTIVITY_REORDER_TO_FRONT. So far so good, the problem comes when I want to bring the activity with an animation using overridePendingTransition.

    Intent i = new Intent(ActivityA.this, ActivityB.class);
    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    ActivityA.this.startActivity(i);
    overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
    

    The transition is not shown, however, if the flag is not added to the intent (removing line 2) then there is no problem.

    Is it possible to bring an activity to front with an animation?

    Thanks a lot!

  • Admin
    Admin over 13 years
    Hmm...sounds like a good idea! thanks a lot. Im going to learn how to deal with animations and try that.
  • GSree
    GSree over 13 years
    @Daniel check out APIDemos (provided along with android framework) com.example.android.apis.animation.Rotation3dAnimation.java and how it is used in Transition3D.java. Should give you good insight.
  • Admin
    Admin over 13 years
    Thanks GSree for your help! I have tried to launch a slide-out animation in the onPause() from activity A, and a slide-in in the onResume() from activity B. It was a nice idea, and it work out, but it is not smooth enough, as you can see a kind of black blink, because the animations are not excuted at the same time. I keep working on this, if I find a good solution I will let it know.
  • GSree
    GSree over 13 years
    Good to know that. Let me know how it goes
  • Ravi
    Ravi about 11 years
    by doing it in onResume(). The effect is not at all smooth when the first activity is brought on top , it appears with a jerk
  • cwhsu
    cwhsu over 10 years
    this works for me when I start a new activity from service, and need to override pending transaition!
  • Daniele Segato
    Daniele Segato almost 10 years
    This suggestion is not correct. onResume is called in many occasion that have nothing to do with a new intent being used. The correct solution is the one I pointed out: do it in onNewIntent() method
  • tomrozb
    tomrozb almost 10 years
    The only proper solution. Should be accepted answer.
  • Daniele Segato
    Daniele Segato almost 10 years
    Why would you do that when onNewIntent() method has been created just for that reason? onCreate(), onStart(), and onResume() serve completely different purposes
  • Daniele Segato
    Daniele Segato over 9 years
    I wish I know why some people downvoted my answer, this is, as tomrozb said, the only proper solution
  • M Penades
    M Penades about 9 years
    in my case, that was exactly my mistake
  • androidguy
    androidguy about 6 years
    Original question didn't state the original activity is being finished. This answer isn't relevant if you're starting one without finishing,
  • androidguy
    androidguy about 6 years
    Since onNewIntent isn't called when the activity is created, you'll have to also do overridePendingTransition where you start the activity.
  • Daniele Segato
    Daniele Segato about 6 years
    That i obvious :-) i never said you need to do it in only one place. And by the way: now navigation Controller should be used
  • Oyeme
    Oyeme about 5 years
    I spent hours looking for a solution. Cheers
  • seekingStillness
    seekingStillness about 4 years
    Does onNewIntent get called on the first load of the Activity? If not, then the animation won't show on the first load.
  • Daniele Segato
    Daniele Segato about 4 years
    @seekingStillness no it does not, onNewIntent() is only for when you re-open an activity with a new intent. For the fist time you can do on onCreate() only if savedInstanceState is null (be sure to use saveInstanceState() so that it will not be null after the first time)