Search code examples
androidkotlinandroid-lifecycle

Why does my main activity get recreated and not resumed when I finish using the top activity?


I've built a simple Android app (new to Android dev and Kotlin) and I'm trying to retain some state of my main activity after starting a second activity. The way I'm starting the second activity is by clicking on a action bar item and then:

val intent = Intent(this, InfoActivity::class.java)
startActivity(intent)

Starting the new Activity works fine, but when I exit the second activity by doing:

override fun onSupportNavigateUp(): Boolean {

    finish()

    return super.onSupportNavigateUp()
}

the main activity will get recreated (i.e. onCreate gets called) but I just want it to resume (i.e. onResume). What do I need to change for it to resume and not get recreated?

And for some strange reason, it seems to get created twice (i.e. it runs through onCreate twice)


Solution

  • for your first activity in AndroidManifest.xml add this

    android:launchMode="singleTop"