Ellen Spertus
03/09/2020, 6:48 PMclass MainActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Metadata.initialize()
}
}
class Metadata {
companion object {
boolean initialized = false
fun initialize(context: Context) {
check(!initialized)
// do stuff (omitted)
initialized = true
}
}
}
Sometimes my app fails because the check
at the beginning of Metadata.initalize()
fails, which implies that the companion object is surviving the destruction of MainActivity
, despite their presumably being in the same process. Could someone help me understand why the companion object stays around even if the activity needs to be re-created?Casey Brooks
03/09/2020, 6:54 PMstatic
fields in Java. They stick around for the entire application lifetime, not for the lifetime of an Activity instance.Zach Klippenstein (he/him) [MOD]
03/09/2020, 6:54 PMEllen Spertus
03/09/2020, 6:56 PMfun initialize(context: Context) {
if (!initialized) {
// do stuff (omitted)
initialized = true
}
}
Casey Brooks
03/09/2020, 6:58 PMEllen Spertus
03/09/2020, 7:00 PMCasey Brooks
03/09/2020, 7:05 PMEllen Spertus
03/09/2020, 7:06 PMcodeslubber
03/09/2020, 7:28 PM