开发者

Android Back button doesn't work with dialog

Pretty simple; The dialog is showing. I press the back button on the phone, nothing happens. I've tried this, but it never gets called:

static void ProgressDialog(Context context)
{
    String text = context.getString(R.string.dialog_loading_video);
    vDialog = new ProgressDialog(context)
    {
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) 
        {

            if (keyCode == KeyEvent.KEYCODE_BACK) 
            {

                // Nothing hap开发者_Python百科pening here!

            }

            return super.onKeyDown(keyCode, event);
        }
    };

    vDialog = ProgressDialog.show(context, "", text);
    vDialog.getWindow().setGravity(Gravity.TOP);
}


First, get rid of the second assignment to vDialog. Then, you need to make your dialog cancelable by calling setCancelable(boolean).


In line vDialog = ProgressDialog.show(context, "", text); you create new Dialog that doesn't have overriden onKeyDown() method. Replace this line with these code:

vDialog.setTitle("");
vDialog.setMessage(text);
vDoalog.show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜