Home » Android » android – `setEnterTransition` only works with `ActivityCompat.startActivity`

android – `setEnterTransition` only works with `ActivityCompat.startActivity`

Posted by: admin June 15, 2020 Leave a comment


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

So I did:

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.

How to&Answers:

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:


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.


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

window.setEnterTransition(new Slide());

and then start activity as follows:



You can add transition while calling intent like this

Intent i = new Intent(context, SampleActivity.class);
i.putExtra("data", data);
context.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);