当按意图启动另一个活动然后返回

时间:2015-05-11 10:08:49

标签: android android-alertdialog

我有一个alertDialog,允许用户转到设置以打开位置。然后,当我从设置应用程序返回时,alertDialog仍然显示,我必须手动关闭它。任何想法如何解决这个问题?这是我的代码:

public void showSettingsAlert() {
    alertDialog = new AlertDialog.Builder(this);
    // Setting Dialog Title
    alertDialog.setTitle("Location settings");
    // Setting Dialog Message
    alertDialog.setMessage("Location service is not enabled. Do you want to go to settings menu?");
    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            dialog.dismiss();
            startActivity(intent);
        }
    });
    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alertDialog.show();
}

2 个答案:

答案 0 :(得分:0)

使用cancel()方法代替dismiss()

alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

        startActivity(intent);
        dialog.cancel();

    }
});

答案 1 :(得分:0)

 // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
            alertDialog.cancel();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                
            startActivity(intent);
        }
    });