如何在AlertDialog上单击“确定”后重新加载主要活动

时间:2013-03-21 07:04:12

标签: java android

我有一个AlertDialog,我想在单击“确定”后重新加载主活动。问题是我单击确定后我的活动返回到主屏幕。

JAVA代码:

private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
        "This application needs GPS satellite or Wireless Networks localization enabled" 
        + "\n" + "Do you want to enable it?")
        .setCancelable(true)
        .setPositiveButton("Yes",
    new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
            startActivity(new Intent(
            Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            finish();
            startActivity(getIntent());
        }

    })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
            dialog.cancel();


        }
    });

    final AlertDialog alert = builder.create();

    alert.show();

}

帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

更改此代码

new DialogInterface.OnClickListener() {
    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
        startActivity(new Intent(
        Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        finish();
        startActivity(getIntent());
    }  

new DialogInterface.OnClickListener() {
    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
        startActivityForResult(new Intent(
        Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
    }

答案 1 :(得分:0)

再次调用onCreate方法。但是将null作为参数传递。

onCreate(null);