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);