当一个片段被换下,换上中后卫堆栈(或删除)它留在记忆? [英] When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?

查看:116
本文介绍了当一个片段被换下,换上中后卫堆栈(或删除)它留在记忆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的方式类似活动工作的行为?例如与活动它的工作原理是这样的:

Is the behavior similar to the way Activities work? For example with Activities it works like this:

活动A 开始的 b活动,而 B 在屏幕上,该系统能够去除的 A 从存储器如果需要由系统。一旦pressing BACK, A 将被重新装入内存,就好像它从来没有离开过摆在首位。

Activity A starts Activity B, while B is on screen, the system is able to remove A from memory if it is needed by the system. Upon pressing BACK, A will be recreated into memory as if it never left in the first place.

我已经看过了所发生的事情记忆聪明与片段明确的解释,并没有发现任何东西。它是否以同样的方式工作?例如:

I have looked for a clear explanation of what happens memory wise with Fragments and haven't found anything. Does it work the same way? For example:

活动ç片f 在其布局。然后,在某些时候的 F 替换 g片段,但 F 保存在它的后面堆栈。

Activity C has Fragment F in its layout. Then, at some point F is replaced by Fragment G, but F is kept in its back stack.

威尔的 F 留在内存中,直到在 C 被杀害还是可以根据需要在系统中删除?

Will F stay in memory until the C is killed or can it be removed by the system as needed?

真的什么,我问的是我是否运行内存溢出的风险,如果我有复杂的片段在一个单一的活动一回栈?

推荐答案

看看这个:<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/app/BackStackRecord.java#BackStackRecord.Op.0fragment">BackStackRecord.Op.fragment

这是如何片段被存储在后面的栈。注意实时参考,既不的WeakReference SoftReference 用于那里。

That is how fragments are stored in the back stack. Note the live reference, neither WeakReference nor SoftReference are used there.

现在这样的:<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/app/FragmentManager.java#FragmentManagerImpl.0mBackStack">FragmentManagerImpl.mBackStack

这就是门店经理后退堆栈。简单的的ArrayList ,也没有WR的或SRS。

That is where manager stores the back stack. Simple ArrayList, also, no WRs or SRs.

最后这一点:<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/app/Activity.java#Activity.0mFragments">Activity.mFragments

这是引用片段经理。

GC可以只收集有没有活引用对象(不是从任何线程访问)。这意味着,,直到你的活动被破坏(等等, FragmentManager 引用就没了), GC将不能收取任何的在后面的片段堆栈

GC can only collect objects that have no live references (are not reachable from any thread). That means, until your Activity is destroyed (and so, FragmentManager reference is gone), GC will not be able to collect any of the Fragments in the back stack.

请注意,当活动被破坏,<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/app/Activity.java#Activity.onSaveInstanceState%28android.os.Bundle%29">retains状态(当你打开设备为横向模式等),它没有实际的保留在堆栈中片段的对象,只有他们的状态 - <一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/app/Fragment.java#FragmentState">Fragment.FragmentState对象,在后面的堆栈,即实际的片段重新创建的每个活动被重新创建与保持状态的时间。

Note that when Activity is destroyed and retains state (like when you turn the device to landscape mode), it doesn't retain actual Fragment objects in the stack, only their states - Fragment.FragmentState objects, i.e. actual fragments in the back stack are re-created every time activity gets re-created with retained state.

希望这有助于。

PS 的因此,简而言之:是,你可以通过添加片段耗尽内存来支持堆叠以及通过加入过多的意见,查看层次结构。

PS So, in short: Yes, you can run out of memory by adding Fragments to back stack as well as by adding too many views to view hierarchy.

UPD 的考虑到你的榜样,<强> F 将保持在内存中,直到 C 被杀害。如果 C 被杀害,然后用不同的配置复活 - F 将被销毁,转生到一个不同的对象,以及。因此, F 的内存占用,直到周围的 C 会丢失状态或后退堆栈被清除。

UPD Considering your example, F will stay in memory until C is killed. If C is killed and then resurrected with different configuration - F will be destroyed and reincarnated in a different object as well. So, F's memory footprint is around until C loses state or back stack is cleared.

这篇关于当一个片段被换下,换上中后卫堆栈(或删除)它留在记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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