I have been trying to learn and play with Android studio for about 3 weeks now. I just came to a situation where AlertDialogue doesn't dismiss on clicking on positive button.
private void showGPSDisabledAlertToUser() {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
builder.setTitle("Turn On Location / GPS");
builder.setCancelable(false);
builder.setMessage("Application Needs To Determine Device's Physical Location.");
builder.setPositiveButton("YES, TURN ON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // This ain't working
goToInternetSettings();
}
});
builder.setNegativeButton("NO, CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
closeApplication();
}
});
builder.create().show();
}
private void goToInternetSettings() {
Intent gpsSetting = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsSetting);
}
private void closeApplication() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
It looks like I am only able to close the dialogue if I have to double click on positive.
On the other hand, with negative button there is no such trouble. I guess, since negative button shuts down the whole application, and hence that is taking care of that problem else it would have been the same.