Android DialogFragment and configuration changes

I am having trouble understanding how to make a simple DialogFragment for editing an (complex) object, for example Person, with a first and last name and a list of email addresses, each of which consists of an enumeration (Work, Home, etc.) And the address .

First of all, how to pass the Person object to DialogFragment correctly? My current solution has the setPerson (Person person) method, which is called after creating DialogFragment, but before dialog.show (). This works fine until a configuration change occurs (user rotates the screen). DialogFragment is recreated and the reference to my Person object is null. I know that I can save an instance using onSaveInstanceState, but the object is complex and expensive, and saving a large object in this way seems wasteful.

I also tried disabling the configuration change in an action using my dialog, and this fixes the problem, but I want the dialog to be reused and requiring that all the actions that use it to disable configuration changes look wrong.

The third option is to save the reference to Person in a static variable, but again, I want the dialog box to be reused and able to support multiple instances.

How do other people handle their expensive and complex objects in a reusable dialog box?

+5
source share
2 answers

Well, there are several solutions, none of which are fantastic or fault tolerant if you cannot serialize the editable object.

I never recommend using android:configChanges="orientation", if absolutely, 100% inevitable. There are other configuration changes, and your application will still work with others if you resort to using this solution.

, , setRetainInstance(true) DialogFragment. . , . , , " ", , . .

+2

- Object Parcelable, Bundle Fragment fragment.setArguments(bundle). onActivityCreated() , getArguments().

, "" , onSaveInstanceState(Bundle state) , onActivityCreated(), savedInstanceState !=null.

Parcelable - "" , , UI, . , setRetainInstance(true) UI Activity.

+1

All Articles