I have one activity called TourMapActivity and StopChoiceDialogFragment.
This is a map route type app. When the user starts the route, this modal opens. If the user clicks on any of the options, when user enters stop 1, the dialog does not appear again, as expected. But when the dialog is dismissed without clicking anywhere, and when the user enters stop 1's radius, the dialog appears again.
I have tried a lot of different ways of retaining the state that the dialog is shown once. But it does not seem to work.
private void onTourDataInitialized() {
Log.d(TAG, "ON TOUR DATA INITIALIZED");
if (tourStopList != null && stopPosition < tourStopList.size()) {
tourStop = tourStopList.get(stopPosition);
} else {
tourStop = null;
finish();
}
Log.d(TAG, "IT IS REACHING TO SHOW STOP CHOICE QUESTION"
+ getIntent().getBooleanExtra(INTENT_EXTRA_SHOW_MAP_CHOICE, false));
boolean showChoice = getIntent().getBooleanExtra(INTENT_EXTRA_SHOW_MAP_CHOICE, false);
Log.d(TAG, "SHOW MAP CHOICE extra = " + showChoice);
if (showChoice) {
getIntent().removeExtra(INTENT_EXTRA_SHOW_MAP_CHOICE);
Log.d(TAG, "REMOVED EXTRA");
showStopChoiceDialog();
}
}
private void showStopChoiceDialog() {
Log.d(TAG, "SHOW STOP CHOICE DIALOG CALLED tourStop="
+ (tourStop != null ? tourStop.getNumber() : "null"));
if (!isGoogleMap) {
Log.d(TAG, "NOT Google Map, returning");
return;
}
Log.d(TAG, "IN THE METHOD");
if (getSupportFragmentManager().findFragmentByTag("stopDialog") == null) {
StopChoiceDialogFragment dialog = new StopChoiceDialogFragment();
dialog.show(getSupportFragmentManager(), "stopDialog");
} else {
Log.d(TAG, "Dialog already showing, skip.");
}
}
public class StopChoiceDialogFragment extends DialogFragment {
public interface StopChoiceListener {
void onListChosen();
void onMapChosen();
void onLocationChosen();
}
private StopChoiceListener listener;
private boolean dialogDismissed = false;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.d("StopChoiceDialog", "onAttach()");
if (context instanceof StopChoiceListener) {
listener = (StopChoiceListener) context;
} else {
throw new ClassCastException(context.toString()
+ " must implement StopChoiceDialogFragment.StopChoiceListener");
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d("StopChoiceDialog", "onCreateDialog()");
LayoutInflater inflater = requireActivity().getLayoutInflater();
View stopDialogView = inflater.inflate(R.layout.layout_stop_dialog_fragment, null);
TextView noButton = stopDialogView.findViewById(R.id.button3);
TextView yesButton = stopDialogView.findViewById(R.id.button2);
TextView locationButton = stopDialogView.findViewById(R.id.button4);
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setView(stopDialogView);
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialog.getWindow() != null) {
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
noButton.setOnClickListener(v -> {
dismiss();
if (listener != null) listener.onListChosen();
});
yesButton.setOnClickListener(v -> {
dismiss();
if (listener != null) listener.onMapChosen();
});
locationButton.setOnClickListener(v -> {
dismiss();
if (listener != null) listener.onLocationChosen();
});
return dialog;
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
Log.d("StopChoiceDialog", "onDismiss()");
dialogDismissed = true; // mark dismissed
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("StopChoiceDialog", "onCreate() new");
// keep instance across config changes (like rotation)
setRetainInstance(true);
// ensures this is displayed as a dialog
setShowsDialog(true);
// optional: prevents dialog from being canceled by back button or outside tap
setCancelable(false);
}
@Override
public void onStart() {
super.onStart();
Log.d("StopChoiceDialog", "onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.d("StopChoiceDialog", "onResume()");
Log.d("StopChoiceDialog", Objects.requireNonNull(getDialog()).toString());
Log.d("StopChoiceDialog", String.valueOf(dialogDismissed));
if (dialogDismissed && getDialog() != null) {
Log.d("StopChoiceDialog", "Force dismissing stale dialog");
dismissAllowingStateLoss();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d("StopChoiceDialog", "onDestroyView()");
}
@Override
public void onDetach() {
super.onDetach();
Log.d("StopChoiceDialog", "onDetach()");
}
}