I create an app from the template, and populate with some Logs as below
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MainApplication.something++
Log.d("Tracking", "onCreate ${MainApplication.something} ${Thread.currentThread()}")
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
Log.d("Tracking", "onStart")
}
override fun onResume() {
super.onResume()
Log.d("Tracking", "onResume")
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
Log.d("Tracking", "onConfigurationChanged")
}
override fun onPause() {
super.onPause()
Log.d("Tracking", "onPause")
}
override fun onStop() {
super.onStop()
Log.d("Tracking", "onStop")
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Log.d("Tracking", "onSaveInstanceState")
}
override fun onDestroy() {
super.onDestroy()
Log.d("Tracking", "onDestroy")
}
}
and
class MainApplication: Application() {
companion object {
var something = 0
}
override fun onCreate() {
super.onCreate()
Log.d("Tracking", "Application onCreate $something ${Thread.currentThread()}")
}
}
When I run it, I notice that sometimes it launch 3 times. (some other times, it launch 2 times and sometimes 1 time).
The log as below
2021-11-04 23:55:31.409 7810-7810/com.example.empty D/Tracking: Application onCreate 0 Thread[main,5,main]
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.409 7810-7810/com.example.empty D/Tracking: Application onCreate 0 Thread[main,5,main]
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.508 7810-7810/com.example.empty D/Tracking: onStart
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume
2021-11-04 23:55:31.508 7810-7810/com.example.empty D/Tracking: onStart
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume
Initially, I thought it's the Activity gets launched 3 times, but then when I log out the Application onCreate, it also 3 times. I explicitly put a static variable something
, and clearly see that the Applications are 3 different instances (since the variable didn't get incremented across the 3 times).
To confirm it is not because configuration change or activity got killed (as per explained in https://stackoverflow.com/a/3588750/3286489), I also log out when onPause, onStop, onSavedInstanceState and onDestroy, none of them get called.
Does anyone know why did the application start 3 times when only launch once?
Note: I compiled on Android SDK 31 and 30, and Emulator API S and API 29, all of them have the same behavior. Also tried on the actual device Android 8.0. Same behavior shown.
Update I notice it only happens after the second compile. Perhaps it's just a repeated uncleared Log (though I have clear it).