I\'ve been having some trouble getting some custom DialogPreference subclasses inside a PreferenceFragment to remain visible when the screen is rotated. I don\'t experience
I have indeed had this issue myself. There is a bug where the DialogFragment doesn't restore state because it is null, or at least it happened to me.
Using multiple sources I eventually got a solution working. Have your dialog extend this BaseDialogFragment:
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.DialogFragment;
import com.actionbarsherlock.app.SherlockDialogFragment;
public class BaseDialogFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState == null || savedInstanceState.isEmpty())
savedInstanceState = WorkaroundSavedState.savedInstanceState;
setRetainInstance(true);
Log.d("TAG", "saved instance state oncreate: "
+ WorkaroundSavedState.savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
if (savedInstanceState == null || savedInstanceState.isEmpty())
savedInstanceState = WorkaroundSavedState.savedInstanceState;
Log.d("TAG", "saved instance state oncretaedialog: "
+ WorkaroundSavedState.savedInstanceState);
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState == null || savedInstanceState.isEmpty())
savedInstanceState = WorkaroundSavedState.savedInstanceState;
Log.d("TAG", "saved instance state oncretaeview: "
+ WorkaroundSavedState.savedInstanceState);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onDestroyView() // necessary for restoring the dialog
{
if (getDialog() != null && getRetainInstance())
getDialog().setOnDismissListener(null);
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle outState)
{
// ...
super.onSaveInstanceState(outState);
WorkaroundSavedState.savedInstanceState = outState;
Log.d("TAG", "saved instance state onsaveins: "
+ WorkaroundSavedState.savedInstanceState);
}
@Override
public void onDestroy()
{
WorkaroundSavedState.savedInstanceState = null;
super.onDestroy();
}
/**
* Static class that stores the state of the task across orientation
* changes. There is a bug in the compatibility library, at least as of the
* 4th revision, that causes the save state to be null in the dialog's
* onRestoreInstanceState.
*/
public static final class WorkaroundSavedState {
public static Bundle savedInstanceState;
}
}
Note that in any subclasses whose methods have a savedInstanceState parameter, you may have to call super with WorkaroundSavedState.savedInstanceState. And when you're restoring state (i.e. in onCreate(), just ignore the savedInstanceState and instead use WorkaroundSavedState.savedInstanceState. The static holder isn't the cleanest solution, but it works. Just make sure to set it to null in your onDestroy().
In any case, my DialogFragment does not disappear when I rotate the screen (and that's without any configChanges). Let me know if this code addresses your issue and if not I'll take a look at what's going on. Also note that I haven't tested this within PreferenceFragment but instead other Fragments from the compatibility class or from ActionBarSherlock.