Android - is onDestroy supposed to destroy the activity, its variables and free up memory

前端 未结 1 1901
长情又很酷
长情又很酷 2021-02-15 02:24

I have a bug in my code that made me think I don\'t fully understand the Android Lifecycle. Yes, I have read all the docs and looked at the diagrams, but they seem to talk only

1条回答
  •  悲哀的现实
    2021-02-15 03:22

    onDestroy is an instance method and any memory it releases (or allows the garbage collector to release) will be of the corresponding instance. Activities are not singleton; there can be more than one instance of an Activity.

    Static variables are class variables and are accesible to all instances of that class. They are initialized when the class is loaded, not when each instance of the class is created.

    Please see Understanding Instance and Class members for more info. An excerpt:

    Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

    0 讨论(0)
提交回复
热议问题