Android的 - 是的onDestroy应该破坏活动,它的变量并释放内存 [英] Android - is onDestroy supposed to destroy the activity, its variables and free up memory

查看:281
本文介绍了Android的 - 是的onDestroy应该破坏活动,它的变量并释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误,在我的code,让我觉得我不完全了解Android的生命周期。是的,我已经阅读所有的文档,看了看图表,但他们似乎只谈论时节省数据,当活动可能会失去焦点或被杀。不过,我的问题是,如果我不需要保存状态,发生了变数和放什么;其存储的值?我希望它们能够被破坏,但在我的code错误似乎表明并非如此。

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.

在我这里的情况是发生了什么事。我有一个启动的自定义视图(没有XML,我只是在屏幕上绘制位图在我的自定义视图)的活动。目前,我有我的活动中唯一的变量是只是我的看法变:GameView gameView;

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;

现在在我看来,我宣布几个位图,简单的整型和浮点型变量来处理绘图和触摸事件,我有含有少量的位图对象的一个​​阵列,每个坐标对象和其他一些东西的。一个在班上的变量此对象的,是重新presents多少对象的是当前计数的静态变量。我就是这么做的,所以对象的实例化导致其轨道而不是跟踪这个对象的类以外的人如何反对他们的。

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.

我预计静态变量留在所有对象相同的值,但我也希望这个变量以及所有其它变量和活动的看法物体被破坏,一旦onDestroyed被称为该活动。然而,这似乎并没有发生。当这个活动再次启动,该静态变量仍包含从上次运行了previous价值 - 尽管onDestroyed被调用。

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.

现在我的问题不是如何解决这一问题(我可以写code不同,以修复这个bug),但我想知道为什么出现这种情况与此静态变量,因为它不是全球整个应用程序,它只存在该活动的看法里面?此外,这让我不知道有关该视图中的变量的其余部分 - 是他们摧毁了他们的记忆释放,或者至少它们的值,下一次不再可用的活动被称为或做我需要做自己 - 即使我并不需要保存任何这种状态下的数据?

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.

推荐答案

的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.

这篇关于Android的 - 是的onDestroy应该破坏活动,它的变量并释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆