什么决定在活动一个片段恢复再制造?片段

2023-09-07 01:48:31 作者:萌城少年╯

有关查看取值为标识,他们将自动保存在调用时 super.onSaveInstanceState(outState);

For Views having IDs, they will be auto-saved when calling super.onSaveInstanceState(outState);.

有关片段取值添加到活动,有什么情况下,这将是重新创建时其活动重新创建(如屏幕旋转),以及哪些情况下,它不会呢?怎么确定?什么是规则?

For Fragments added to an Activity, what are the cases that it will be re-created when its Activity is re-created (e.g. screen rotation), and what are the cases it will not? What to determine? What is the rule?

到目前为止,我已经尝试了以下情况。但审判正误差并不意味着任何规则或解决方案。的

情况下,当片段 s的重新存储的:

Cases when Fragments are re-stored:

在正常情况下: FragmentTransaction.add()同一个ID布局 片段没有用户界面: FragmentTransaction.add()标签只 Normal case: FragmentTransaction.add() to the layout with an ID. Fragment without a UI: FragmentTransaction.add() to a tag only

情况下,当片段是不会重新存储:

Cases when Fragments are NOT re-stored:

super.onSaveInstanceState(outState); 跳过 在一个UI恢复片段取值不匹配的ID。 When super.onSaveInstanceState(outState); is skipped. Restored Fragments in a UI without a matching ID.

什么是一般规则?什么我错过了文档?

What is the general rule? Anything i missed in the documentation?

在此先感谢。

据我了解,和实验,所有动态(编程)加入片段,则保存在他们的活动调用 super.onSaveInstanceState(outState)

To my understanding, and experiments, ALL dynamically (programmatically) added Fragments are saved upon their Activity calling super.onSaveInstanceState(outState).

所有动态(编程)加入片段取值包括: 片段 s的只是一个标签(没有UI), 片段取值连接到查看(带UI), 和片段 s的的两个的一个用户界面和标签。 的(有没有什么别的类型?)的 ALL dynamically (programmatically) added Fragments includes, Fragments with only a tag (no UI), Fragments attached to a View (with UI), and Fragments with both an UI and a tag. (is there any types else?)

关于恢复片段与UI成一个布局,没有匹配的ID,在片段的确是重新-created。它只是不能直观地被显示在布局,用下面的警告消息:

Regarding restoring a Fragment with an UI into a layout that with no matching ID, the Fragment is indeed re-created. 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.

在我们回到与其匹配的ID的用户界面,它会被正确地恢复。

Once we are back into an UI with its matching ID, it will be restored correctly.

推荐答案

我有同样的问题,你可以在这里看到:后屏幕旋转,findFragmentById()返回一个片段,即使有布局里面没有这样的ID

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

在 http://developer.android Android的开发者文档。 COM /培训/基础知识/片段/ communicating.html 报价如下:

The Android Developer Documentation at http://developer.android.com/training/basics/fragments/communicating.html quotes this:

在配置变化导致承载这些活动   片段重新启动,它的新实例可以使用不同的布局,   不包括在同一片段作为所述previous布局。在这   情况下,所有的previous碎片仍会被实例化和   在新实例中运行。然而,任何将不再说   在视图层次标记不会有关联   其内容视图中创建并返回false从isInLayout()。   (在code在这里也展示了如何确定一个片段放置   在一个容器中不再布局运行与容器   并避免产生其观点,层次在这种情况下。)

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(). (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.