0

I have a DialogFragment listing data generated at Activity.onCreate(Bundle). When the Activity is recreated on, for example, orientation change, DialogFragment is recreated using old data from previous Activity instance.

DialogFragment is recreated at Activity's super.onCreate(savedInstanceState) using savedInstanceState. How to modify savedInstanceState so that new data is used to recreate DialogFragment?

pat
  • 48
  • 9

1 Answers1

1

This probably should work.

In fragment class

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
user345280
  • 1,920
  • 2
  • 18
  • 24
  • This only recreates `DialogFragment` using old data from previous `Activity` instance. When the `Activity` is being recreated, new data is generated at `Activity.onCreate(Bundle)`. `DialogFragment` should be recreated using those new data. – pat Jul 14 '16 at 19:38
  • So you are having different data in activity every time orientation changes? And you want to pass it to dialog fragment every time? – user345280 Jul 14 '16 at 19:48
  • Yes. Data may be different in each activity recreation. – pat Jul 14 '16 at 20:05
  • So why you are using data from savedInstanceState to recreate dialogFragment then you already have new data? – user345280 Jul 14 '16 at 20:11
  • I didn't override `onCreate`/`onSaveInstanceState` in `DialogFragment`. It is automatically recreated when calling `super.onCreate(savedInstanceState)` in the `Activity`. I can stop the recreation by calling `super.onCreate(null)`, but it is an overkill. – pat Jul 14 '16 at 20:25
  • Try to detach and attach your fragment. – user345280 Jul 14 '16 at 20:56
  • I find out it is not necessary to detach and attach the `DialogFragment` to recreate the `Views`, as `DialogFragment`'s `onCreateDialog` is called after `Activity`'s `onCreate`. The problem is at `DialogFragment`'s `onActivityCreated`, which recovers saved `Dialog` state even when new data is applied. Override `onActivityCreated` to call `super.onActivityCreated(null)` solves the problem. – pat Jul 17 '16 at 18:56