在一个 Android Honeycomb Activity 启动另一 Activity 后,如何释放 Drawable 内存?

发布于 2024-12-12 07:40:47 字数 641 浏览 0 评论 0原文

我有一个 Android Honeycomb 应用程序,有两个活动;第一个有一个大的Bitmap(从资源中获取)作为主LinearLayout的背景,我发现将此元素的背景设置为null< Activity 的 onStop() 方法中的 /code> 可以节省大量内存,实际上大约 5MB。

然而,这似乎只有当我单击设备上的“睡眠”按钮时才起作用。如果我这样做,分析器就会显示 5MB 的大小会随您的喜好而消失。如果我启动第二个活动,则第一个活动的 onStop() 直到第二个活动的 onCreate() 之后才会被命中,并且探查器建议 BitmapDrawable 毕竟没有从内存中删除——所以它就坐在那里,五个看不见的无用兆字节,把这个地方弄得乱七八糟。

我可以从 onPause() 中删除它,这可能会更好,但我还使用各种 Dialog 元素的 onStop() 方法也将它们的背景绘图清空。他们似乎没有 onPause() 方法。

当一个活动启动另一个活动时,是否有推荐的方法来摆脱这些大型的、消耗内存的元素?

I have an Android Honeycomb application with two activities; the first one has a large Bitmap (taken from resources) as a background to the main LinearLayout and I have discovered that setting the background of this element to null from the activity's onStop() method can save a lot of memory—around 5MB, in fact.

However, this only seems to work when I click the "sleep" button on the device. If I do that the profiler shows the 5MB drops out of the world as easy as you like. If I start the second activity, the first activity's onStop() doesn't get hit until after the second activity's onCreate() and the profiler suggests that the BitmapDrawable is not removed from memory after all—so there it sits, five invisible useless megabytes, cluttering the place up.

I could remove this from onPause(), which might work better, but I'm also using the onStop() methods of various Dialog elements to null their background drawables as well. They don't seem to have onPause() methods.

Is there a recommended method for getting rid of these kinds of large, memory-consuming elements as an activity launches another one?

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

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

发布评论

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

评论(2

2024-12-19 07:40:47

对我来说,OnPause 事件也是放置“无内存”代码的更好地方。

这里是从父活动打开子活动时的事件流

-- Open caller activity -- 
[ActCaller] 1311090884303: onCreate
[ActCaller] 1311090884572: onStart
[ActCaller] 1311090884699: onPostCreate savedInstanceState null
[ActCaller] 1311090884802: onResume
[ActCaller] 1311090884908: onPostResume
-- Open child activity --
[ActCaller] 1311090926270: onSaveInstanceState
[ActCaller] 1311090926374: onPause
  [ActChild] 1311090926556: onCreate
  [ActChild] 1311090926703: onStart
  [ActChild] 1311090926807: onPostCreate savedInstanceState null
  [ActChild] 1311090926911: onResume
  [ActChild] 1311090927014: onPostResume
[ActCaller] 1311090927508: onStop

正如您已经注意到的,父活动的 onStop 仅在子活动创建完成后才会被调用。

从两个不同的位置调用无内存代码怎么样?考虑到对话框通常不会完全覆盖调用活动,因此保留其背景是有意义的。
也许当您打开新活动而不是对话框时可以设置一个标志,并且 OnPause 逻辑仅在启动新活动而不是对话框时才会删除背景。

Also for me OnPause event is a better place where put your "memory-free" code.

Here the event flow when you open a child activity from a parent activity

-- Open caller activity -- 
[ActCaller] 1311090884303: onCreate
[ActCaller] 1311090884572: onStart
[ActCaller] 1311090884699: onPostCreate savedInstanceState null
[ActCaller] 1311090884802: onResume
[ActCaller] 1311090884908: onPostResume
-- Open child activity --
[ActCaller] 1311090926270: onSaveInstanceState
[ActCaller] 1311090926374: onPause
  [ActChild] 1311090926556: onCreate
  [ActChild] 1311090926703: onStart
  [ActChild] 1311090926807: onPostCreate savedInstanceState null
  [ActChild] 1311090926911: onResume
  [ActChild] 1311090927014: onPostResume
[ActCaller] 1311090927508: onStop

As you already noticed, parent's onStop is called only after complete child creation.

What about calling memory-free code from two different location? Consider that dialogs, generally, don't completely cover calling activity, so preserving it's background has a sense.
Maybe a flag can be set when you open a new activity instead of a dialog, and OnPause logic will drop background only when a new activity is launched, and not a dialog.

有木有妳兜一样 2024-12-19 07:40:47

没有特殊的方法来处理这些事情,它是常规的java方式:

对象会在未来未知的时间被垃圾回收,如果它们是
不再引用。

只要不要保留任何你不需要的东西的引用,就可以了。

查看此视频以获取有关此内容的详细信息:

http://www.youtube.com/watch?v= _CruQY55HOk

There is no special way for handling such things, it's regular java way:

Objects are garbage collected at an unknown time in future if they are
no longer referenced.

Just don't keep a reference to anything you don't need, and you will be fine.

Check this video for great info on this:

http://www.youtube.com/watch?v=_CruQY55HOk

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