`setEnterTransition` only works with `ActivityCompat.startActivity`
Asked Answered
P

2

9

I want to add an enter transition to the next activity.

So I did:

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());

This doesn't seem to work. After doing some trial and error (as I had this transition working on other activities) I found out that it did work after calling

ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, "some_name");
ActivityCompat.startActivity(activity, new Intent(TourAndLoginActivity.this, LoginActivity.class), activityOptionsCompat.toBundle());

But I have no shared element (I added a view just to test it). It is not possible to add 'null' as shared element.

Is it really mandatory to do it this way? My workaround would be to add an invisible shared element.

Posthaste answered 3/6, 2016 at 7:3 Comment(1)
for me following is working: startActivity(new Intent(this, Activity1.class), ActivityOptionsCompat.makeSceneTransitionAnimation(this, null, null).toBundle());Sway
S
13

taken from android developers documentation:

Start an activity using transitions If you enable transitions and set an exit transition for an activity, the transition is activated when you launch another activity as follows:

startActivity(intent,
          ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

If you have set an enter transition for the second activity, the transition is also activated when the activity starts. To disable transitions when you start another activity, provide a null options bundle.

https://developer.android.com/training/material/animations.html

So first enable the transitions as you are already doing like the following:

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());

and then start activity as follows:

startActivity(intent,
          ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
Sway answered 8/6, 2016 at 11:0 Comment(4)
dammit! I did not see that the makeSceneTransitionAnimation() shared element arguments are varargs. How could I miss that. Thanks!Posthaste
Yes right I should have written that also in the answer :PSway
What if I want the enter transition to work on Launcher activity? There is no way to call startActivity with bundle for launcher activity.Spenser
@Spenser interesting question. I have never tried that. Better to ask a new question for this case. I will also try this.Sway
B
-1

You can add transition while calling intent like this

Intent i = new Intent(context, SampleActivity.class);
i.putExtra("data", data);
startActivity(i);
context.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
Britton answered 3/6, 2016 at 7:18 Comment(1)
Yes, that is true. So I've written my slide in / slide out xml animation, but then I need one to have no transition on the current activity. If you supply '0', the activity disappears. And I need to do this override again in the 'onBack' to slide down the current activity. Not what I want. I'd rather use a hidden dummy shared elementPosthaste

© 2022 - 2024 — McMap. All rights reserved.