警报在意图之前自动关闭

时间:2015-03-17 06:39:00

标签: android android-intent

我想在我的活动中意图之前显示警告。点击"确定"按钮只有意图应该工作但在我的情况下警告框显示然后立即意图工作。我的意思是警告在意图之前自动关闭。 这是我的活动代码

 alert.showAlertDialogpostive(ChangePass.this, "Successfully Submited",
                                    "Password Changed Successfully", true);

                        UserFunctions userFunction = new UserFunctions();
                        userFunction.logoutUser(getApplicationContext());
                           Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
                           // Close all views before launching Dashboard
                           dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                           startActivity(dashboard);
                           // Close Registration Screen
                           finish();

这是我的警报构建器类

 @SuppressWarnings("deprecation")
    public void showAlertDialogpostive(Context context, String title, String message,
            Boolean status) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);
        alertDialog.setCancelable(false);
        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.success);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int which) {
                alertDialog.dismiss();
            }
        });

        // Showing Alert Message
        alertDialog.show();

    }

只有在警告框中单击“确定”按钮后,Intent才能正常工作。帮助我!

1 个答案:

答案 0 :(得分:1)

UserFunctions userFunction = new UserFunctions();
                    userFunction.logoutUser(getApplicationContext());
                       Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
                       // Close all views before launching Dashboard
                       dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(dashboard);
                       // Close Registration Screen
                       finish();

将上面的代码移到警报的按钮内。所以你的代码如下所示,

@SuppressWarnings("deprecation")
public void showAlertDialogpostive(Context context, String title, String message,
        Boolean status) {
    final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);
    alertDialog.setCancelable(false);
    // Setting Dialog Message
    alertDialog.setMessage(message);

    if(status != null)
        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.success);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int which) {
           UserFunctions userFunction = new UserFunctions();
                    userFunction.logoutUser(getApplicationContext());
                       Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
                       // Close all views before launching Dashboard
                       dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(dashboard);
                       // Close Registration Screen
                       finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();

}