Android DialogFragment not persisting on orientation change

I'm just trying to keep the dialog in Android orientation, which I thought would be simple. But it doesn't work as I thought. I have a simple member class of mine Activity

called StartMonitoringDialogFragment

that extends DialogFragment

. In my activity, I am showing this as:

    StartMonitoringDialogFragment dialog = new StartMonitoringDialogFragment();
    dialog.show(getFragmentManager(), getClass().getName() + "StartDialog");

      

However, the dialog is not displayed when the orientation is changed. I noticed that the fragments onCreateView are correctly called after the orientation change and correctly create and return its view (the fragment instance variables are still set), however nothing is displayed. Shouldn't this be displayed? do i need to track it manually?

Edit I tried to solve this problem by adding

    Fragment dialog;
    if(savedInstanceState != null && (dialog = getFragmentManager().findFragmentByTag(getClass().getName() + "StartDialog")) != null) {
        ((DialogFragment)dialog).show(getFragmentManager(), getClass().getName() + "StartDialog");
    }

      

for my action onCreate(Bundle savedInstanceState)

and it initially seemed to work, however now I am constantly encountering an exception java.lang.IllegalStateException: Fragment already added

. Any ideas what to do?

+3


source to share


1 answer


This is a known issue that happens even without library support for whatever reason.

Anyway, one workaround I've found to work is adding it to each dialog chunk (or the basic one that will all expand, of course):



@Override
public void onDestroyView() {
    //workaround for this issue: https://code.google.com/p/android/issues/detail?id=17423 (unable to retain instance after configuration change)
    if (getDialog() != null && getRetainInstance())
        getDialog().setDismissMessage(null);
    super.onDestroyView();
}

      

+3


source







All Articles