9

I have an Activity and need to show Dialog in it. Everything works fine here. I've overrided onCreateDialog method in Activity, here is the code:

@Override
protected Dialog onCreateDialog(int dialog)
{
    if(dialog == 10)
    {
        if(waitDialog != null)
            waitDialog.dismiss();

        dialogCreated = true;
        waitDialog = CreateWaitDialog(this); 
        return waitDialog;
    }
    else
        return new Dialog(this);
}

Where CreateWaitDialog is custom method of creating a dialog and waitDialog is static variable.

I'm showing dialog by calling showDialog(10)

All code is executing fine.

After dialog has been shown, i'm closing it by calling.

if(waitDialog != null)
    waitDialog.hide();

And i'm dismissing it when Activity is destroyed.

if(dialogCreated)
        dismissDialog(10);
    super.onDestroy();

It's closing and everything is great. BUT, when I change the orientation of my device and Activity is recreated it pops up again by himself! I'm not calling any showDialog or something like that it is just popping up!

4 Answers 4

11

I think this is defined behavior of the Activity and onCreateDialog:

Callback for creating dialogs that are managed (saved and restored) for you by the activity. The default implementation calls through to onCreateDialog(int) for compatibility. If you are targeting HONEYCOMB or later, consider instead using a DialogFragment instead.

If you use showDialog(int), the activity will call through to this method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing.

If you would like the activity to manage saving and restoring dialogs for you, you should override this method and handle any ids that are passed to showDialog(int).

I will guess that onDestroy is too late in the activity lifecycle to dismiss the dialog. My guess is that the Activity is saving your dialog inside onSaveInstanceState.

I might try dismissing the dialog inside onSaveInstanceState before calling super.onSaveInstanceState, then the dialog will be dismissed before android tries to save and restore it.

@Override
onSaveInstanceState(Bundle outstate) {

    dismissDialog(10);  
    super.onSaveInstanceState(outstate);
}
5
  • 1
    I hate competing answers since you took the extra time to cite the documentation, I removed my answer and gave you an upvote.
    – Sam
    Oct 24, 2012 at 19:24
  • @Sam I think your answer still has value, I would leave it. He probably really should be calling dismiss instead of hide, my answer is merely a guess/hack to get around the default behavior of onCreateDialog. I think yours is probably the better solution.
    – Tim
    Oct 24, 2012 at 19:26
  • I just double-checked the source code and if the dialog is displayed repeatedly, using hide() is a faster approach. Also your answer maybe have been a guess, but it is well founded and correct. So I'm happy to leave my upvote.
    – Sam
    Oct 24, 2012 at 19:32
  • 1
    Glad it helped. Note that if you rotate the screen before the dialog is dismissed it will disappear, not sure if that's your intention or not.
    – Tim
    Oct 24, 2012 at 19:37
  • I handled it with some flags :) Is there any more elegant solution ?
    – Eramir
    Oct 24, 2012 at 19:49
6

I found myself with a similar problem that occurred because I overrode onDismiss() without calling super.onDismiss(). If you find yourself with dialogs that reappear when an activity is recreated, you may want to verify that you did not make the same mistake.

1
  • oh, Thank you Mate!
    – AlexS
    Nov 8, 2018 at 12:41
3

Call Activity.removeDialog(int id) if you want to prevent the app from continuing to save and restore your dialog.

1
  • This approach tolerates dialogs not having been displayed (dismissDialog throws an error) so any and all dialogs which might be displayed with onCreateDialog can be removed.
    – user2711811
    Oct 13, 2018 at 1:16
0

I have encountered this problem today, following to all the messages I can found in before's answers, I can't make the dialog not show again.

for my case, I show dialog and dismiss it whenever user press the button on dialog, and I finish the Activity immediately after it, when I back and re-create that Activity, the dialog not show yet, but once I press the button to go to share image to another app, then back from that app, the dialog show(Android api level 12).

the code like this, not other code between this two lines:
button.setOnClickListener {
    Dialog.dismiss()
    Activity.finish()
}

I override any restore methods I can find but none of them call:

class Activity {
    // savedInstanceState always null and other two method never call
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.i("DialogManager", "onCreate:$savedInstanceState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        Log.i("DialogManager", "onRestoreInstanceState:$savedInstanceState")
        super.onRestoreInstanceState(savedInstanceState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        Log.i("DialogManager", "onRestoreInstanceState:$savedInstanceState persistentState:$persistentState")
        super.onRestoreInstanceState(savedInstanceState, persistentState)
    }
}

public class MyDialogFragment extends androidx.fragment.app.DialogFragment {
    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        // this method call but savedInstanceState always null
        super.onViewStateRestored(savedInstanceState);
        Log.i("DialogManager", "onViewStateRestored:" + savedInstanceState);
    }
}

class OkCancelNoTitleDialog() : MyDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // all of them null
        savedInstanceState?.run {
            Log.i("DialogManager", "onCreateView savedDialogState:${getBundle("android:savedDialogState")}")
            getBundle("android:savedDialogState")?.let {
                Log.i("DialogManager", "onCreateView dialogHierarchy:${it.getBundle("android:dialogHierarchy")}")
                Log.i("DialogManager", "onCreateView dialogShowing:${it.getBundle("android:dialogShowing")}")
            }
        }
    }

I set a break point to investigate the Activity.removeDialog(int id) that dane point in his answer, but the mManagedDialogs is empty:

class Activity {
    @Deprecated
    public final void removeDialog(int id) {
        if (mManagedDialogs != null) {
            final ManagedDialog md = mManagedDialogs.get(id);
            if (md != null) {
                md.mDialog.dismiss();
                mManagedDialogs.remove(id);
            }
        }
    }
}

keep investigating, I try to delay the finish that was mention on the first code snippet:

Dialog.dismiss()
postDelay({
    Activity.finish()
}, 1000)

the problem gone. seem it is too quick to invoke finish(), we should let the dismiss() done their job or Activity will keep something for the Dialog to re-show it automatically.

just a workaround solution, not perfect, after that I try to find out the best way, I saw the dismiss() of Dialog do something work, send a Message at last, so I observe that message to do finish() Activity, but I failure, the problem still.

Handler handler = new Handler() {
    @Override
    public void handleMessage(@NonNull Message msg) {
        Log.i("DialogManager", "receive message:" + msg.what);
        Activity.finish()
    }
};
Message message = Message.obtain(handler, 19430);
Log.i("DialogManager", "setup message:" + message.what);
dialog.setDismissMessage(message);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.