Reputation: 60184
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?
Upvotes: 5
Views: 2466
Reputation: 6517
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();
}
}
Upvotes: 3