androidkotlinrecreate

recreate activity with animation not work


Change all my activity to ViewModel to keep the UI data because I need to recreate the activity when switching to dark mode or applying a theme.

I created the fade in and fade out animations to add them, but I can't get it.

I already tried

recreate()
overridePendingTransition(in, out)

finish()
overridePendingTransition(in, out)
startActivity(intent)
overridePendingTransition(in, out)

Even if it is not with animation, when recreating the activity the transition is very different from when, for example, the device is rotated and the activity is configured with config-changes="orientation"


Solution

  • I solved doing a kotlin extension:

    fun Activity.recreateSmoothly() {
        finish()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        startActivity(Intent(this.intent))
    }
    

    and just calling in the activities.