3

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 ?

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.