Lifetime of a static variable in Android

前端 未结 14 2259

when I declare and initialize a variable as static in my main activity and the activity gets destroyed. Can I still access the content of the variable?

For example t

14条回答
  •  青春惊慌失措
    2020-12-03 10:23

    The value of static variables will persist as long as the class is loaded - it has almost nothing to do with Activity lifecycle (onCreate, ..., onDestroy)

    The first time you access a class from code it will get loaded and then it won't go away until there is a reason to unload it.

    Android will unload a class if your app gets completely removed from memory - either via a task-killer or when your app is no longer active and memory gets low.

    So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens: 1. the class is unloaded 2. the JVM shuts down 3. the process dies

提交回复
热议问题