Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment with setRetainInstanceState(true) is not displayed after the device is rotated

Tags:

android

I have a question regarding DialogFragment. I am trying to make a dialog that keeps it's state after the device is rotated. This dialog has a bunch of references to things such as adapters and other heavier objects and I need this to be kept upon rotation, if possible without having to make every reference Parcelable or Serializable in order for me to use onSaveInstanceState to save and restore them when the original activity is re-created.

I've noticed there's a method called setRetainInstance(boolean) on the DialogFragment which allows you to keep the dialog fragment instance when the activity is re-created. However, when I rotate the device now, the dialog is not showing anymore. I know I can get it from the activity's FragmentManager, but I cannot find a way to make it visible again. Any suggestions on this?

Thanks, Mihai

like image 875
r1k0 Avatar asked Dec 18 '12 14:12

r1k0


People also ask

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.


1 Answers

There are few things you need to do :

  1. use instance factory method to initiate a DialogFragment instance like this :

    public static MyDialogFragment newInstance(MyModel model) {
        MyDialogFragment myDialogFragment = new MyDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("MODEL", model);
        myDialogFragment .setArguments(bundle);
        return myDialogFragment;
    }
    
  2. by putting setRetainInstance(true) in onCreate, all of your references declared in the fragment will be kept after the original activity is re-created

    @Override
    public void onCreate(Bundle icicle) {
        this.setCancelable(true);
        setRetainInstance(true);
        super.onCreate(icicle);
    
    }
    
  3. avoiding disappear on rotation by doing this

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);
        super.onDestroyView();
    

    }

  4. get your object by using

    (MyModel) getArguments().getSerializable("MODEL")
    
like image 148
ricky888 Avatar answered Oct 25 '22 19:10

ricky888