Question

I've looked at several of the questions pertaining to handling an orientation change in a DialogFragment, and the common theme seems to be that the askers aren't getting any answers, at none that I understand...

So, I'd like to take a different approach... Is there a way I can programmatically dismiss the dialog... Looking at my LogCat, I can see that if the dialogfragment is active, and the screen orientation is changed, then my Activity restarts.

Along the way, the DialogFragment is also restarted, but this happens after onCreate and before onResume (as close as I can tell). It's fairly simple for me to detect this in my dialog, and what I'd like to do is abort the dialog view creation in this case. What I've tried so far is like this:

    if (normal_conditions_detected)
    {
        View v = inflater.inflate (R.layout.dialog_layout, container, false);
        final Dialog d = getDialog();
        d.setTitle (R.string.sensor_config);

        ... more stuff
        return v
    }
    else
    {
        getDialog().cancel();
        return null;
    }

This does avoid the null pointer nonsense I was getting, but now I get what I can only describe as an empty dialog, even with the cancel() command in there. Is there a way I can get my dialogFragment code to refuse to create the view?

Was it helpful?

Solution

When you rotate your device, activity executes

onSaveInstanceState 

method and save the current state which means it saves your fragment state as well, after recreation it executes

onCreate(Bundle savedInstanceState)

method again, savedInstanceState holds your old data, in here you can make something like

if (savedInstanceState == null){
   YourDialogFragment f = new YourDialogFragment()
   f.show // etc
}

Only once your fragment will be created, instead of putting a control inside fragment, you can control it by the activity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top