简体   繁体   English

通过意图启动另一个活动然后返回时,AlertDialog不会自行关闭

[英]AlertDialog doesn't close by itself when launching another activity by intent and then returning

I have an alertDialog which allows user to go to settings to turn on location. 我有一个alertDialog,允许用户转到设置以打开位置。 Then, when I come back from settings app, alertDialog is still showing and I have to close it manually. 然后,当我从设置应用程序回来时,alertDialog仍然显示,我必须手动将其关闭。 Any ideas how to solve this ? 任何想法如何解决这个问题? This is my code: 这是我的代码:

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();
}

Use cancel() method instead of dismiss() : 使用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();

    }
});
 // 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);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM