Search code examples
androidandroid-activityandroid-transitions

ExitTransition doesn't work if not set up in onCreate


I would like to set up my exit transition with specific target views (addTarget) which I only know after the user has clicked an item, therefore I instantiate it only before starting the new activity.

However, this way the exit transition is not applied at all, event without the addTarget calls. When I navigate back and start the activity with transition again, it's working well.

private fun onItemClick(id: Long) {
  window.exitTransition = Slide(Gravity.LEFT)

  val activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(this, ..).toBundle()

  val intent = Intent(this, SettingsActivity::class.java)
  ActivityCompat.startActivity(this, intent, activityOptions)
}

Can I make the transition work also for the first time?


Solution

  • The exit transition must be set before calling ActivityOptions.makeSceneTransitionAnimation to make it work.

    I figured it out by looking at these classes:

    ActivityTransitionCoordinator

    ExitTransitionCoordinator