Android OnOptions ItemSelected dialog.show()未显示

Android OnOptions ItemSelected dialog.show()未显示,android,android-menu,Android,Android Menu,我试图显示一个对话框,在成功注销时通知用户。当前,该对话框仅在处理OnOptions ItemSelected方法后显示。我想在OnOptions ItemSelected期间或在OnOptions ItemSelected中运行该对话框 我在谷歌上搜索并尝试了“this”、“MainActivity.this”、“break”、“returntrue”。还没开始工作。请给我一些建议 public boolean onOptionsItemSelected(MenuItem item) {

我试图显示一个对话框,在成功注销时通知用户。当前,该对话框仅在处理OnOptions ItemSelected方法后显示。我想在OnOptions ItemSelected期间或在OnOptions ItemSelected中运行该对话框

我在谷歌上搜索并尝试了“this”、“MainActivity.this”、“break”、“returntrue”。还没开始工作。请给我一些建议

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case LOGOUT_ID: {
            //run some logout process 
            // ........................
            //show message
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Successfully logged out");
            dialog.setPositiveButton("Ok", new   
            DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                 redirectToLoginPage();}
            });
            dialog.show();  
            break;
        }
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
试试这个

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
     case LOGOUT_ID: 
        doLogOut();
         break;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
}




private void showAlertDialogLogOut() {

    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("Logout");
    dialog.setMessage("Successfully loged out");
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
           redirectToLoginPage()
        }
    });
    dialog.show();

}

private void doLogOut() {

    // Do your job and when finish show the dialog
    showAlertDialogLogOut();
}

我希望您在注销过程中不要完成该活动。您在注销过程中会做什么?可能是您应该发布您的
onCreateOptions菜单方法和菜单布局。我猜您希望在注销作业完成之前显示对话框?如果这是真的,我想只要把show对话框代码移到上面的
//运行一些注销过程就可以了works@Modge在该过程中,我将自定义应用程序上下文的对象设置为null