I have an android app that has a timer that when expires plays a sound.
I have the following code to capture when a user tries to navigate away from the fragment;
public void onPause() {
super.onPause();
if (gameOn == 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("Are you sure you want to cancel the match?")
.setCancelable(false)
.setTitle("Cancel match?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
The problem I have is I want to add in an additional option (YES / NO, which I know how to do) that suppresses the user from navigating away without making the choice too (the UI could mean a user accidentally navigates away from the fragment, this is a fail safe, are you really sure question).
onPause does trigger the alert dialog to pop-up but behind it the new fragment has already loaded and as far as I'm aware onPause is the first of the events triggered when a user moves away from a fragment.
All help appreciated.