简体   繁体   English

在Android中的AlertDialog中按下确定后返回MainActivity

[英]Go back to MainActivity when OK pressed in AlertDialog in Android

I am using this alert dialog to pop-up a message saying that no Active internet connection is available. 我正在使用此警报对话框弹出一条消息,指出没有可用的有效Internet连接。

I want to get back to the MainActivity when the user click's OK but i can't seem to get this going. 当用户点击“确定”时我想回到MainActivity,但我似乎无法做到这一点。

package com.xx.xx.xxhelper;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;


public class AlertDialogManager {

public void showAlertDialog(Context context, String title, String message,
        Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

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

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
    }

    });

    // Showing Alert Message
    alertDialog.show();
}
}

I added 我补充道

public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(AlertDialogManager.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)    
startActivity(intent);
}
});

But i get: 但我得到:

  1. The constructor Intent(AlertDialogManager, Class) is undefined 构造函数Intent(AlertDialogManager,Class)未定义
  2. The method startActivity(Intent) is undefined for the type new DialogInterface.OnClickListener(){} 方法startActivity(Intent)未定义类型new DialogInterface.OnClickListener(){}

any clues? 任何线索?

Pass in the Activity context (to start activity when OK is clicked on the alert dialog) using something like the following: 传入Activity上下文(在单击警报对话框时单击OK时启动活动),使用以下内容:

public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(context, HomeActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
}

You should use the context passed: 您应该使用传递的上下文:

 public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(context, HomeActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
      context.startActivity(intent);
    }
 });

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

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