Restauración de referencias a objetos de fragmentos después de cambios de configuración

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 "Deep Conical" 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 ?

preguntado el 24 de agosto de 12 a las 19:08

1 Respuestas

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

  • The Arguments passed to fragment using setArguments() seem to be retained.
  • La llamada a la Fragment Método onAttach() is guaranteed. Data exchanges and event registering with parent goes here. The parent can be Activity u otro Fragment so checking Fragment.getParentFragment() puede ser necesario.
  • 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.

Respondido el 03 de Septiembre de 13 a las 10:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.