简体   繁体   English

重新确定Activity时如何确定片段还原?

[英]What to determine a Fragment restore upon Activity re-create?

For View s having IDs, they will be auto-saved when calling super.onSaveInstanceState(outState); 对于具有ID的View ,在调用super.onSaveInstanceState(outState);时将自动保存它们super.onSaveInstanceState(outState); .

For Fragment s added to an Activity , what are the cases that it will be re-created when its Activity is re-created (eg screen rotation), and what are the cases it will not? 对于添加到Activity Fragment ,在重新创建其Activity (例如屏幕旋转)时将重新创建它的情况是什么? What to determine? 确定什么? What is the rule? 规则是什么?

So far, i have tried the following cases. 到目前为止,我已经尝试了以下情况。 But trial-n-error does not mean any rules or solutions. 但是,试错法并不意味着任何规则或解决方案。

Cases when Fragment s are re-stored: 重新存储Fragment的情况:

  1. Normal case: FragmentTransaction.add() to the layout with an ID. 正常情况:将FragmentTransaction.add()到具有ID的布局中。
  2. Fragment without a UI: FragmentTransaction.add() to a tag only 没有UI的FragmentTransaction.add()FragmentTransaction.add()仅用于标签

Cases when Fragment s are NOT re-stored: 重新存储Fragment的情况:

  1. When super.onSaveInstanceState(outState); super.onSaveInstanceState(outState); is skipped. 被跳过。
  2. Restored Fragment s in a UI without a matching ID. 没有匹配ID的UI中还原的Fragment

What is the general rule? 一般规则是什么? Anything i missed in the documentation? 我在文档中错过了什么?

Thanks in advance. 提前致谢。


Edit: 编辑:

To my understanding, and experiments, ALL dynamically (programmatically) added Fragment s are saved upon their Activity calling super.onSaveInstanceState(outState) . 据我了解和实验,所有动态(以编程方式)添加的Fragment都保存在其Activity调用super.onSaveInstanceState(outState)

  • ALL dynamically (programmatically) added Fragment s includes, 所有动态(以编程方式)添加的Fragment包括,
    • Fragment s with only a tag (no UI), Fragment s仅带有标签(没有UI),
    • Fragment s attached to a View (with UI), Fragment附加到View (带有UI),
    • and Fragment s with both an UI and a tag. Fragment s的两者的用户界面和一个标签。
    • (is there any types else?) (还有其他类型吗?)

Regarding restoring a Fragment with an UI into a layout that with no matching ID, the Fragment is indeed re-created. 关于将具有UI的Fragment还原到没有匹配ID的布局中,确实可以重新创建该Fragment It just cannot be shown visually in the layout, with the following warning message: 只是无法在布局中直观显示,并显示以下警告消息:

04-08 11:41:22.445: W/PhoneWindow(9853): Previously focused view reported id 2131165226 during save, but can't be found during restore.

Once we are back into an UI with its matching ID, it will be restored correctly. 一旦返回具有匹配ID的UI,它就会正确还原。


i am still looking forward to some reliable references and your opinions! 仍然期待着一些可靠的参考资料和您的意见!

I had the same problem, you can see it here: After screen rotation, findFragmentById() returns a fragment, even if there's no such ID inside the layout 我遇到了同样的问题,您可以在这里看到它: 屏幕旋转后,即使布局内没有这样的ID,findFragmentById()也会返回一个片段

The Android Developer Documentation at http://developer.android.com/training/basics/fragments/communicating.html quotes this: http://developer.android.com/training/basics/fragments/communicating.html上的Android开发者文档引用了以下内容:

When a configuration change causes the activity hosting these fragments to restart, its new instance may use a different layout that doesn't include the same fragments as the previous layout. 当配置更改导致托管这些片段的活动重新启动时,其新实例可能会使用不同的布局,该布局不包含与先前布局相同的片段。 In this case all of the previous fragments will still be instantiated and running in the new instance. 在这种情况下,所有先前的片段仍将被实例化并在新实例中运行。 However, any that are no longer associated with a tag in the view hierarchy will not have their content view created and will return false from isInLayout(). 但是,不再与视图层次结构中的标记关联的任何对象都不会创建其内容视图,并且将从isInLayout()返回false。 (The code here also shows how you can determine if a fragment placed in a container is no longer running in a layout with that container and avoid creating its view hierarchy in that case.) (此处的代码还显示了如何确定放置在容器中的片段是否不再在该容器的布局中运行,并且在这种情况下避免创建其视图层次结构。)

That means, we have to check screen orientation than believing in null pointer checks. 这意味着,我们必须检查屏幕方向,而不是相信空指针检查。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM