恢复配置更改后的碎片的对象引用 [英] Restoring object references of fragments after configuration changes

查看:114
本文介绍了恢复配置更改后的碎片的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动复杂的数据对象。当用户想要编辑的数据该对象的显示样片段的浮动对话框。这个片段具有引用数据对象的某些部分。用户使活编辑从该片段的数据。一些简单的文本编辑,例如。

在用户编辑这些数据,配置变更发生的情况。(著名的方向变化)。一旦变化,每一件事情是自动系统,包括浮动片段,就像是之前重新创建。

我不知道为什么,Android的应该刚刚重新启动Activty,为什么保存并重新加载为手动之后创建的东西吗?但是,这是另外一个问题。

因此​​,虽然片段的新克隆又回来了,它已经失去了的连接的对数据对象。由于该数据对象的没了,我们有一个新的使用失去了所有用户编辑。我要保存状态只是配置更改之前,之后恢复它。

  1. 如何在活动确定它是否是由用户发起 (应该从头开始)或正在重建由系统上的配置。变更(应重装 保存状态)?

  2. 在这之后,如何重新建立片段的连接 数据对象,以便用户可以继续编辑像什么事也没 发生了什么?

解决方案

对于大多数的问题,我发现了一些好方法:

  • 通过使用 setArguments()来片段参数似乎被保留。
  • 片段通话方法 onAttach()有保证。数据交换和事件与母公司注册放在这里。家长可以为活动或其他片段所以检查 Fragment.getParentFragment()可能是必要的。
  • AsyncTaskLoader 的保留,并自动重新连接由系统。数据读取/处理code的片段放在这里。
  • 在使用自定义数据管理器类,碎片不持有引用的数据对象,而他们的数字ID的,就像在一个数据库中。
  • 而不是方法的直接引用调用,Command对象都可以使用。
  • 让家长活动广播事件从一个片段到所有的子片段,它可以作为独立的用户。

I have a complex data object in my Activity. A floating dialog like fragment is shown when user wants to edit data for that object. This fragment has references to some parts of data object. User makes live edits to that data from this fragment. Some simple text editing, for example.

While user is editing this data, a configuration change happens.(the famous Orientation change). Upon change, every thing is automatically recreated by system including the floating fragment just like it was before.

I don't know why, Android should have just restarted the Activty, why save and reload something that was manually created afterward ? But that's another question.

So, while fragment's new clone is back, it has lost that "Connection" to the data object. Because that data object's gone too and we have a new one with all user edits lost. I want to save the state just before configuration change and regain it afterwards.

  1. How can an Activity determine if it is being launched by user (should start from scratch) or is being recreated by system on a config. change ( should reload saved state) ?

  2. And after that, how to re-establish the fragment's connection to data object so the user can continue editing like nothing ever happened ?

解决方案

For most of the question, I found some good ways:

  • The Arguments passed to fragment using setArguments() seem to be retained.
  • The call to Fragment method onAttach() is guaranteed. Data exchanges and event registering with parent goes here. The parent can be Activity or another Fragment so checking Fragment.getParentFragment() may be needed.
  • AsyncTaskLoader's are retained and automatically re-connected by system. Data fetching/processing code for fragment goes here.
  • Using custom data manager classes, fragments do not hold references to data objects , rather their numerical id's, like in a database.
  • Instead of method calls from direct references , Command objects can be used.
  • Let Parent Activity broadcast events from one Fragment to all child Fragments, which can act as independent subscribers.

这篇关于恢复配置更改后的碎片的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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