简体   繁体   English

从backstack调用片段时会发生什么?

[英]What happens when a fragment called from backstack?

I have a container fragment.I load fragment inside of it.When i wanna load a new fragment i use this code 我有一个容器片段。我在其中加载片段。当我想加载一个新片段时,我使用此代码

FragmentTransaction ft=activity.getSupportFragmentManager().beginTransaction();
                    ft.add(R.id.mainframe,new_fragment_here).hide(getCurrentFragment()).addToBackStack(null).commit();

What I'm asking is when i hide a fragment can it be removed by garbage collector when memory is less? 我要问的是,当我隐藏一个片段时,如果内存不足,垃圾收集器可以将其删除吗?

And when i want to get back to old fragment i simply use getFragmentManager().popBackStack(); 当我想回到旧片段时,只需使用getFragmentManager().popBackStack();

When i use this code does my fragment and its images or videos permanantly destroyed? 当我使用此代码时,我的片段及其图像或视频会被永久破坏吗?

Does this kind of usage cause out of memory error? 这种用法会导致内存不足错误吗?

I have to say nearly all my fragment show images from web with glide.Some fragments only show one image and others show more than that with recyclerview. 我不得不说几乎我所有的片段都显示了来自网络的图像,有些片段只显示一个图像,而其他片段则显示更多。

I believe what you are trying to ask is about the lifecycle of the Fragment and how GC work's for Fragment in the back-stack. 我相信您要问的是有关Fragment的生命周期以及GC在后端堆栈中如何工作的信息。

From my observation and knowledge when you add a Fragment to the backstack then Fragment is in the RAM of the device but if there are many number of task(Fragment) alive then GC might do a force destroy of the Fragment based on the need. 根据我的观察和知识,当您将Fragment添加到Backstack时,该Fragment位于设备的RAM中,但是如果有许多活着的task(Fragment),则GC可能会根据需要强行破坏Fragment。

For better understanding of the fragment lifecycle and what happens to the Fragment in Backstack refer this Flow chart. 为了更好地了解片段的生命周期以及Backstack中的片段发生了什么,请参考此流程图。

This diagram might be little complex but it will help you understand how fragment work's. 该图可能有点复杂,但可以帮助您了解片段的工作原理。

NOTE: when you pop/replace a fragment then the fragment itself is destroyed you can check it by keeping a breakPoint at onDestroy() of the Fragment. 注意:弹出/替换片段时,片段本身会被破坏,您可以通过在片段的onDestroy()处保留一个breakPoint来对其进行检查。

片段生命周期

If image is not available refer here 如果图像不可用,请参考此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从后台弹出时未调用 Fragment 的 onResume() - Fragment's onResume() not called when popped from backstack 从Backstack重新创建片段时未处理onResume() - Handle onResume() not called when recreating fragment from backstack Android:从backstack弹出片段时调用哪种方法 - Android : Which method is called when fragment is pop out from backstack 自定义加载程序-当片段从堆栈返回时调用onStartLoading() - Custom Loader - onStartLoading() called when fragment returned from backstack 问题实施 LiveData:观察总是在恢复片段时调用(从另一个片段返回堆栈)? - Problem Implement LiveData : Observe always called when resume fragment (backstack from another fragment)? 流行的backstack上的Android Support Fragment叫什么方法? - Android Support Fragment on pop backstack what method is called? 从 Backstack 添加 Fragment 时出现 IllegalStateException - IllegalStateException when adding Fragment from Backstack Android-从Backstack返回时的空白片段 - Android - Blank fragment when returning from backstack 从backStack还原片段时RecyclerView为空 - RecyclerView is empty when restoring fragment from backStack 当片段从 backstack 弹出时调用哪些生命周期方法 - Which lifecycle methods are called when a fragment is popped off backstack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM