Android - onDestroy 应该销毁 Activity、其变量并释放内存

发布于 2024-10-08 06:49:42 字数 813 浏览 0 评论 0原文

我的代码中有一个错误,让我认为我没有完全理解 Android 生命周期。是的,我已经阅读了所有文档并查看了图表,但它们似乎只讨论何时保存数据、何时活动可能会失去焦点或被杀死。但是,我的问题是,如果我不需要保存状态,变量和变量会发生什么?他们的存储值?我预计它们会被销毁,但我的代码中的错误似乎表明情况并非如此。

就我而言,这就是发生的事情。我有一个启动自定义视图的活动(没有 xml,我只是在自定义视图的屏幕上绘制位图)。我当前在活动中拥有的唯一变量只是我的视图的变量:GameView gameView;

现在在我看来,我声明了几个位图、简单的 int 和 float 变量来处理绘图和触摸事件,并且我有一个对象数组,其中包含小位图、每个对象的坐标和一些其他内容。我的类中该对象的变量之一是一个静态变量,它表示当前对象的数量。我这样做了,所以对象的实例化导致它跟踪它们的人对象,而不是在对象的类之外跟踪它。

我期望静态变量在所有对象中保持相同的值,但我也期望一旦为该 Activity 调用 onDestroyed,该变量就会与该 Activity 视图的所有其他变量和对象一起被销毁。然而,这似乎并没有发生。当此 Activity 再次启动时,此静态变量仍包含上次运行时的值 - 即使调用了 onDestroyed。

现在我的问题不是如何解决这个问题(我可以以不同的方式编写代码来解决这个错误),但我想了解为什么这个静态变量会发生这种情况,因为它对于整个应用程序来说不是全局的,它只存在于内部该 Activity 的视图?另外,这让我想知道该视图中的其余变量 - 它们是否被破坏并释放了它们的内存,或者至少它们的值在下次调用活动时不再可用,或者我是否需要自己执行此操作 - 即使我不需要保存任何状态数据?

感谢您对此有任何见解。

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 about when to save data, when the activity may loose focus or get killed. However, my question is if I don't need to save state, what happens to the variables & their stored values? I expected them to be destroyed to, but a bug in my code seems to indicate otherwise.

In my case here is what happened. I have an activity that launches a custom view (no xml, I just draw bitmaps on the screen in my custom view). The only variable I currently have in my activity is just a variable for my view: GameView gameView;

Now in my view, I declare several bitmaps, simple int and float variable to deal with drawing and on touch events and I have one array of objects that contain small bitmaps, coordinates of each objects and a few other things. One of the variables in my class for this object, is a static variable that represents the current count of how many objects their are. I did it this way, so the instantiation of the objects causes it to track how man objects their are, instead of tracking this outside the object's class.

I expected the static variable to stay the same value across all objects, but I also expected this variable to be destroyed along with all the other variables and objects of that Activity's view once onDestroyed was called for that Activity. However, that doesn't seem to happen. When this Activity is launched again, this static variable still contains its previous value from its last run - even though onDestroyed was called.

Now my question is NOT how to fix this (I can write code differently to fix this bug), but I would like to understand why this happens with this static variable, since it isn't global to the whole application, it only exists inside that Activity's view? Also, this makes me wonder about the rest of the variables in that view - are they destroyed and their memory released or at least their values no longer available the next time the activity is called or do I need to do this myself - even though I didn't need to save any of this state data?

Thanks for any insight into this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-10-15 06:49:42

onDestroy 是一个实例方法,它释放(或允许垃圾收集器释放)的任何内存都将属于相应的实例。活动不是单例的;一项活动可以有多个实例。

静态变量是类变量,该类的所有实例都可以访问。它们在加载类时初始化,而不是在创建类的每个实例时初始化。

有关详细信息,请参阅了解实例和类成员。摘录:

有时,您想要变量
是所有对象共有的。这
是通过静态完成的
修饰符。具有静态的字段
他们的声明中的修饰符是
称为静态字段或类
变量。他们与
类,而不是任何
目的。类的每个实例
共享一个类变量,该变量位于
内存中的一个固定位置。任何
对象可以改变类的值
变量,但类变量也可以
无需创建即可被操纵
类的实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文