onBackPressed shows the alertDialog. On clicking the quit button it shows the Main Activity. Again when back key is pressed alertDialog is shown but this time on clicking quit button closes the application. What am I missing?
Here is the OpenExitDialog() Code.
public void OpenExitDialog() {
try {
com.heyzap.sdk.ads.InterstitialAd.fetch();
if (com.heyzap.sdk.ads.InterstitialAd.isAvailable()) {
com.heyzap.sdk.ads.InterstitialAd.display(MainActivity.this);
} else {
start_AppAd.loadAd(new AdEventListener() {
@Override
public void onReceiveAd(Ad ad) {
start_AppAd.showAd(new AdDisplayListener() {
@Override
public void adHidden(Ad ad) { }
@Override
public void adDisplayed(Ad ad) { }
@Override
public void adClicked(Ad arg0) { }
@Override
public void adNotDisplayed(Ad arg0) { }
});
}
@Override
public void onFailedToReceiveAd(Ad ad) {
}
});
}
try {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Exit");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Thankyou!")
.setCancelable(false)
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this,
@Override
public void onBackPressed() {
AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Do you want to exit application?")
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
finish();
//close();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
Hope it's useful.
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
System.exit(0); //add this line
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.