I am launching a DialogFragment
from my Fragment and listening to an event in MainActivity
when the button is pressed in Dialog Fragment.
This is the listener interface defined in DialogFragment
:
public interface NewDialogListener {
public void onDialogPositiveClick(String data);
}
Instantiating the listener in DialogFragment
:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NewDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
currentActivity = getActivity();
newDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(positiveButtonViewOnClickListener);
...
return newDialog;
}
Firing the listener when positive button in DialogFragment
is clicked:
private DialogInterface.OnClickListener positiveButtonOnClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick("positive");
}
};
And then I capture the listener in MainActivity
:
@Override
public void onDialogPositiveClick(String status) {
Fragment fragment = getVisibleFragment();
if (fragment instanceof NewListFragment) {
((NewListFragment)fragment).updateView();
}
}
This works if I haven't changes the rotation of the device. But If I changes the rotation of the device and do the same thing again, the control never reaches onDialogPositiveClick
.
What is that changes when device is rotated that could cause this?