I have a DialogFragment
which do some work by means of listener
which I pass to it on its creation. Listener is a Fragment
instance which implements required interface. Everything is fine but on orientation change everything is recreating and I'm missing listener
, so just bumping into NullPointeException
. How to handle this situation? Should I just close the DialogFragment
if orientation change happens? I don't think users will like this behavior. So I need to recreate a listener
... but how?
Asked
Active
Viewed 2,431 times
5

Eugene
- 59,186
- 91
- 226
- 333
1 Answers
3
Why not use the Fragment#setTargetFragment
method. Like so
public class Fragment1 extends Fragment {
...
public void createFragment2(){
final Fragment dialogFragment = new MyDialogFragment();
dialogFragment.setTargetFragment(this);
dialogFragment.show();
}
}
public class Fragment2 extends DialogFragment{
...
public void onEvent(){
((Fragment1)getTargetFragment()).onEvent();
}
}

asenovm
- 6,397
- 2
- 41
- 52
-
1Can you explain, when I set `setRetainInstance(true)` in `onCreate()` method of my DialogFragment - it just dismisses on configuration change. – Eugene Jul 20 '12 at 14:13
-
1I found, this is an issue. http://code.google.com/p/android/issues/detail?id=17423#c1 So this approach doesn't work, the question is still opened. – Eugene Jul 20 '12 at 14:36
-
also we can set Interface in Fragment2 and do like this http://stackoverflow.com/questions/13238959/how-to-get-button-clicks-in-host-fragment-from-dialog-fragment – LOG_TAG Nov 14 '13 at 05:50