0

I am using a activity with a dialog theme in which I don't put positive nor negative buttons. This activity is launched by the receipt of external notifications (Firebase). When I am about to dismiss the dialog, it takes two taps outside the dialog to dismiss. Indeed:

  • on the first tap the dialog dismisses, but the screen remains covered by a sort of overshadowing film/glaze (it must be the same "shadow" that appears when a dialog is open);
  • on the second tap this sort of shadow disappears, so returning to my initial GUI.

What can I do to avoid tapping twice, dismissing the dialog activity just by one tap? Is there a way to avoid the creation of that shadow covering my GUI, when the dialog activity gets created?

I think my issue is different from the one here: AlertDialog does not dismiss, takes twice tap to close. In this latter post, what is remarked is that the methods setPositiveButton() and setNegativeButton() have an implicit call to the dialog dismissal, that is not what I am trying to figure out. I simply receive a notification, this notification gets turned into an activity with dialog theme, and to dismiss it i need two taps, whereas I would like to have just one tap to dismiss. This is my Activity:

public class NotificationDialogActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        showDialog(this, intent);
    }

    private void showDialog(Context context, @NonNull Intent intent) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(intent.getStringExtra("title"));
        builder.setMessage(intent.getStringExtra("text"));
        builder.create().show();
    }
}
tom
  • 159
  • 2
  • 10
  • 2
    Possible duplicate of [AlertDialog does not dismiss, takes twice tap to close](https://stackoverflow.com/questions/46917355/alertdialog-does-not-dismiss-takes-twice-tap-to-close) – Amine Messaoudi May 06 '19 at 13:19
  • might not be related to your issue but calling `.create()` before `.show()` is useless as `show()` call `create()` already. If no additional processing is needed, just call `.show()` – Kilarn123 May 06 '19 at 13:36
  • @Kilarn123, I have just tried, but doesn't work :) – tom May 06 '19 at 13:39
  • it wasn't supposed to be an answer :) just to say that the create is useless here – Kilarn123 May 06 '19 at 13:42
  • sorry, work in progress about english learning :D thanks – tom May 06 '19 at 13:46

2 Answers2

1

I think the problem may be that you are launching an Activity and then launching a dialog from within that activity. (?) You may want to add an OnCancelListener and finish the activity there. (?)

private void showDialog(Context context, @NonNull Intent intent) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(intent.getStringExtra("title"));
    builder.setMessage(intent.getStringExtra("text"));
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            NotificationDialogActivity.this.finish();
        }
    });
    builder.show();
}

Alternatively, unless you need this activity for some other reason, you could get rid of it entirely and just move your showDialog code to the calling activity and then do that instead.

mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
  • It works! :) I didn't know that firstly it is launched the Activity and then the dialog from within, I thought both are in the same action :) thanks :) – tom May 06 '19 at 14:58
  • @tom Alternatively, unless you need this activity for some other reason, you could get rid of it entirely and just move your showDialog code to the calling activity and then do that instead. – mikejonesguy May 06 '19 at 15:01
  • I found very comfortable this my modality. I don't have a calling activity: what creates and opens my activity dialog is the receipt of a Firebase notification (so using the Service containing the callback onMessageReceived()) :) of course I am open to other solutions :) – tom May 06 '19 at 15:06
  • @tom, Ah. Then this is probably appropriate as is. Glad it works for you. – mikejonesguy May 06 '19 at 15:07
0
  builder.setCancelable(true);
  //Use above line to dismiss a dialog of "AlertDialog.Builder"
  //if you touch outside...
  // It Works..

  //If you use "AlertDialog" then use below line 
  //to dissmiss on touching the outside...
  builder.setCanceledOnTouchOutside(true);
Arwy Shelke
  • 333
  • 1
  • 2
  • 12
  • I cannot use setCanceledOnTouchOutside(true). look at https://developer.android.com/reference/android/app/AlertDialog.Builder – tom May 06 '19 at 13:59