我想从我的onPause()方法中解除我的警报框,因为我正在检查互联网连接,如果没有连接,我会显示一个警告框,说没有连接。但是当我最小化应用程序并重新启动它时,之前的警报框就在那里,所以总共有两个警报框在那里我想避免那样。所以如果我在onPause()方法中解雇它,第二个就不会来了。
任何人都可以帮助我,谢谢你。
if (!haveNetworkConnection) {
// Toast.makeText(getApplicationContext(), "No Connection",
// Toast.LENGTH_SHORT).show();
advtlayout.setVisibility(View.GONE);
System.out.println("no network");
showGraphOverlay();
hideOutputTextView();
hideProgressBar();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
InterPreterWithAnimation.this);
// Setting Dialog Title
alertDialog.setTitle("No Network");
// Setting Dialog Message
alertDialog.setMessage("Turn on Wifi or Mobile data");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to invoke YES event
InterPreterWithAnimation.this
.startActivity(new Intent(
Settings.ACTION_SETTINGS));
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
答案 0 :(得分:2)
首先在全球定义AlertDialog.Builder builder
并尝试在dialog
中检查dialog!=null
对象onPause()
然后
if (dialog.isShowing()) {
dialog.dismiss();
}
答案 1 :(得分:1)
创建AlertDialog的全局对象,然后如果对话框已经显示,则在dialog.dismiss()
方法中调用onPause()
答案 2 :(得分:1)
尝试以这种方式创建AlertDialog
:
AlertDialog.Builder mAlertDialogBuilder = new Builder(mContext);
mAlertDialogBuilder.setTitle("Your title here...");
mAlertDialogBuilder.setMessage("Your message here...");
mAlertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
mAlertDialog = mAlertDialogBuilder.create();
mAlertDialog.show();
然后,你可以在onPause()
内解雇:
if(mAlertDialog.isShowing())
{
mAlertDialog.dismiss();
}
另外,将您的AlertDialog
变量声明为您的班级成员。
答案 3 :(得分:0)
在类
中创建一个实例变量private AlertDialog alertDialog;
然后使用以下代码显示警报。
alertDialog = AlertDialog.Builder(MyActivity.this)
.setTitle("Title");
.setMessage("Message")
[...]
.show();
然后在onPause中,您可以使用以下代码关闭alertDialog
:
@Override
protected void onPause() {
super.onPause();
if (alertDialog != null) {
alertDialog.dismiss();
}
}
答案 4 :(得分:0)
首先:创建 AlertDialog 实例,以便它可以从该类的任何位置进行访问,如下所示...
private AlertDialog mNetworkConnectionDialog = null;
@Override
protected void onCreateActivity(Bundle savedInstanceState){
第二:您没有在 onClick 方法之前添加 @Override 表示法。而不是 alertDialog.show(); ,您应该通过以下方式将 alertDialog 传递给 mNetworkConnectionDialog ......
mNetworkConnectionDialog = alertDialog.create();
不是将 InterPreterWithAnimation.this 传递给AlertDialog.Builder构造函数,您也只能传递 this 。在这里,我正在给出更正版本的代码......
if (!haveNetworkConnection) {
// Toast.makeText(getApplicationContext(), "No Connection",
// Toast.LENGTH_SHORT).show();
advtlayout.setVisibility(View.GONE);
System.out.println("no network");
showGraphOverlay();
hideOutputTextView();
hideProgressBar();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
InterPreterWithAnimation.this);
// Setting Dialog Title
alertDialog.setTitle("No Network");
// Setting Dialog Message
alertDialog.setMessage("Turn on Wifi or Mobile data");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
InterPreterWithAnimation.this.startActivity(new Intent(
Settings.ACTION_SETTINGS));
dialog.dismiss();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.dismiss();
}
});
// Showing Alert Message
mNetworkConnectionDialog = alertDialog.create();
mNetworkConnectionDialog.show();
}
第三:由于您希望在 onPause 方法调用时关闭对话框,因此您应该编写mNetworkConnectionDialog.dismiss();如下......
@Override
protected void onPause() {
if (mNetworkConnectionDialog != null) {
mNetworkConnectionDialog.dismiss();
}
}