Alert dialog shown but not visible

会有一股神秘感。 提交于 2019-12-22 07:59:19

问题


I have an activity where I start the built-in camera using onActivityResult. After taking a picture, I go back to my application and show a pop up asking to the user if he wants to take more pictures or no. It works fine, but after taking the picture, when I press the "save" button on the built-in camera app, and inmediatly I press the home button, If I go back to my application, my activity is visible but not active and the popup that should be visible is there but I cannot see it. If I press the back button and cancel the pop up, my activity is active again, but I dont want to allow the user cancelling the pop up, so when this behavior occurs, I cannot use my app, I just have to kill it...

The question is, how can I force to the dialog being always on the top if it is shown? Cause seems like it is behind the activity, waiting for the user to interact with it...

Thanks!


回答1:


The easiest way I've found to fix this is to track the lifetime of the dialog in the activity and do a hide()/show() in the onResume for the activity. This solution only works for a single Dialog up at a time but could easily be adapted to more if required.

1) Make your activity implement Dialog.OnDismissListener.
2) Add an instance variable for the current Dialog in your Activity:

private Dialog currentDialog = null;

3) In onResume() add:

if(currentDialog != null) {
    currentDialog.hide();
    currentDialog.show();
}

4) For each dialog created in onCreateDialog(), add:

dialog.setOnDismissListener(this);
currentDialog = dialog;

5) Finally, add:

@Override
public void onDismiss(DialogInterface dialog) {
    if(dialog == currentDialog)
        currentDialog = null;
}

That seems to fix it for me.




回答2:


Seems your dialog is cancelable. Try dialog.setCanceleabe(false) and in onResume dialog.show() (for assuring that it will be visible after resuming activity).




回答3:


Old question, I know, but you should add a title: If the layout does not have a height or width it can get downscaled to 0*0 -> Invisible



来源:https://stackoverflow.com/questions/6858047/alert-dialog-shown-but-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!