'Dismiss dialog after screen orientation change

1) I launch a background task (via AsyncTask)

new FindJourneyTask().execute(); // FindJourneyTask extends AsyncTask

2) Still in the main thread (just before new thread is launched) I create a dialog with showDialog(dialogId)

// this method is in FindJourneyTask
protected void onPreExecute() {
    showDialog(DIALOG_FINDING_JOURNEY);
}

3) Screen orientation changes and the Activity is recreated

4) How can I now dismiss the dialog from the FindJourneyTask? Calling dismissDialog(dialogId) does nothing.

// this method is in FindJourneyTask
protected void onPostExecute(FindJourneyResult result) {
    dismissDialog(DIALOG_FINDING_JOURNEY); // does nothing
}


Solution 1:[1]

There is a better solution to this problem now which involves using fragments.

If you create a dialog using DialogFragment, then this fragment will be responsible for maintaining your dialog's lifecycle. When you show a dialog, you supply a tag for your fragment (DialogFragment.show()). When you need to access your dialog, you just look for the necessary DialogFragment using FragmentManager.findFragmentByTag instead of having a reference to the dialog itself.

This way if device changes orientation, you will get a new fragment instead of the old one, and everything will work.

Here's some code based also in @peresisUser answer:

public void onSaveInstanceState(Bundle outState) {
   AppCompatActivity activity = (AppCompatActivity) context;
   FragmentManager fragmentManager = activity.getSupportFragmentManager();
   DialogFragment dialogFragment = (DialogFragment) fragmentManager.findFragmentByTag("your_dialog_tag");
   if(dialogFragment!=null) {
      Dialog dialog = dialogFragment.getDialog();
      if(dialog!=null && dialog.isShowing()) {
         dialogFragment.dismiss();
      }
   }
}

Solution 2:[2]

This is long after the question was asked and answered, but i stumbled upon this problem also and wanted to share my solution...

I check in onSavedInstance() which runs on orientation change, whether the dialog is showing or not with dialog.isShowing(), and pass it into outState variable. Then in your onCreate(), you check this var if it's true. If it is, you simply dismiss your dialog with dialog.dismiss()

Hope this helps others :()

Solution 3:[3]

I tried adding setRetainInstance(true); on OnCreate function of DialogFragment. This will cause dialog to dismiss on rotation.

Solution 4:[4]

Just add this line to specific activity in your Manifest to solve this problem android:configChanges="orientation|screenSize|smallestScreenSize"

like this,

        <activity
            android:name=".PDFTools"
            android:exported="false"
            android:configChanges="orientation|screenSize|smallestScreenSize"
            android:theme="@style/Theme.DocScanner.NoActionBar" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 debihiga
Solution 2
Solution 3 4b0
Solution 4 EAS