Restoring object references of fragments after configuration changes

后端 未结 1 1872
眼角桃花
眼角桃花 2021-02-11 01:55

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

1条回答
  •  灰色年华
    2021-02-11 02:22

    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.

    0 讨论(0)
提交回复
热议问题