Reputation: 3994
I want to dismiss my alert-box from my onPause() method because, I am checking the internet connection and if no connection is there I am showing an alert-box saying no connection. But when I minimize the app and relaunch it the previous alert-box is present there so in total two alert-boxes will be there I want to avoid that.So if I am dismissing it in the onPause() method the second one will not come up.
Can anybody help me with this, Thank you.
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();
Upvotes: 1
Views: 435
Reputation: 9700
First: create AlertDialog instance so that it can access from any where in that class as follows...
private AlertDialog mNetworkConnectionDialog = null;
@Override
protected void onCreateActivity(Bundle savedInstanceState){
Second: you didn't put @Override notation before onClick method. And instead of alertDialog.show(); you should pass your alertDialog to mNetworkConnectionDialog by creating as follows...
mNetworkConnectionDialog = alertDialog.create();
Instead of passing InterPreterWithAnimation.this into AlertDialog.Builder constructor you can also pass only this. Here, I'm giving the corrected version of code...
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();
}
Third: Since you want dismiss your dialog when onPause method called then you should write mNetworkConnectionDialog.dismiss(); as follows...
@Override
protected void onPause() {
if (mNetworkConnectionDialog != null) {
mNetworkConnectionDialog.dismiss();
}
}
Upvotes: 0
Reputation: 25267
Try creating AlertDialog
this way:
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();
Then, you can dismiss inside onPause()
as:
if(mAlertDialog.isShowing())
{
mAlertDialog.dismiss();
}
Also, declare your AlertDialog
variable as your class member.
Upvotes: 1
Reputation: 3506
Create an instance variable in the class
private AlertDialog alertDialog;
And then use the following code for displaying the Alert.
alertDialog = AlertDialog.Builder(MyActivity.this)
.setTitle("Title");
.setMessage("Message")
[...]
.show();
Then in the onPause you can dismiss the alertDialog
using the following code:
@Override
protected void onPause() {
super.onPause();
if (alertDialog != null) {
alertDialog.dismiss();
}
}
Upvotes: 0
Reputation: 21
Create global object of AlertDialog and then call dialog.dismiss()
in onPause()
method if dialog is already showing
Upvotes: 1
Reputation: 47807
First defined AlertDialog.Builder builder
Globally and try to check dialog
object dialog!=null
in onPause()
and then
if (dialog.isShowing()) {
dialog.dismiss();
}
Upvotes: 2