0

For some reason when I display an AlertDialog - the alert is displayed however neither button responds when pressed. Note: I am calling the alert from a push notification (not sure if it makes a difference.)

Source: Setup.java

import cn.pedant.SweetAlert.SweetAlertDialog;

...

public class Setup extends FragmentActivity implements OnMenuItemClickListener{

...



    public void newAlert() {
         SweetAlertDialog alert;
        alert = new SweetAlertDialog(Setup.getInstance(), SweetAlertDialog.NORMAL_TYPE);
        alert.setTitleText("Cruise Planners Mobile");
        alert.setContentText("Currently No Agents Available");
        alert.setCancelable(true);
        alert.setCancelText("Cancel");
        alert.setConfirmText("Retry");
        alert.showCancelButton(true);
        alert.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sDialog) {
                sDialog.dismiss();
                DataManager.clientChatSendRequest(Setup.getInstance());

            }
        });
        alert.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sDialog) {
                sDialog.dismiss();
            }
        });
        alert.show();
    }

Source: GCMIntentService.java

...
public class GCMIntentService extends GCMBaseIntentService {

...


 Setup alert = new Setup();

 alert.newAlert();

...

https://github.com/pedant/sweet-alert-dialog

David Wasser
  • 85,616
  • 15
  • 182
  • 239
  • could you show the definition of `SweetAlertDialog` class? – mic4ael Dec 02 '16 at 14:48
  • not a problem (posted) – Droidthusiast Dec 02 '16 at 15:05
  • I can tell you the issue is not with the SweetAlertDialog class (I can call it from other areas within the app successfully - it is only when calling it from the intent it is not working) – Droidthusiast Dec 02 '16 at 15:37
  • If you try to do alert.newAlert() from an activity. everything's working fine? – T.S Dec 02 '16 at 15:46
  • yes - that is correct – Droidthusiast Dec 02 '16 at 15:52
  • You are trying to show a `Dialog` from a `Service`. That can't work. Dialogs can only be shown from an `Activity`. Also, you can't create an `Activity` (like your `Setup`) using `new`. Only the Android framework can instantiate an `Activity`. The only way to start an `Activity` is to call `startActivity()` with a suitable `Intent`. – David Wasser Dec 02 '16 at 16:06
  • If that is the case - why is the AlertDialog appearing?! – Droidthusiast Dec 02 '16 at 17:10
  • It appears, and shows the text I'd like to display - I simply cannot dismiss it. – Droidthusiast Dec 02 '16 at 17:11
  • I don't know why the Dialog appears. It shouldn't. But without an `Activity` it is meaningless. You need an `Activity` to show a `Dialog`. – David Wasser Dec 05 '16 at 18:42
  • The buttons do not respond because your `Dialog` doesn't receive any keyboard events because the `Dialog` is not associated with the `Activity` that is in the foreground. The `Activity` in the foreground is getting the keyboard events. You can use a Dialog-themed `Activity` if you want. This looks like a `Dialog`, but is really an `Activity`. To show that you would need to call `startActivity()` instead of building and showing the `Dialog`. – David Wasser Dec 07 '16 at 22:30
  • It is possible to show a `Dialog` as a system alert (without an `Activity`), but to get button presses from it is more complicated. See http://stackoverflow.com/questions/4481226/creating-a-system-overlay-window-always-on-top – David Wasser Dec 07 '16 at 22:31
  • You absolutely positively cannot create an instance of an `Activity` (in your case `Setup` is an `Activity` using the `new` keyword. On the Android framework can instantiate an `Activity`, as the `Activity` needs its `Context` set up and only the Android framework can do that. – David Wasser Dec 07 '16 at 22:34

1 Answers1

0

Try to add this line before alert.show():

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

and add to your manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
T.S
  • 871
  • 6
  • 21
  • tried it - same result (AlertDialog appears but neither button responds and the breakpoint is never hit when touched/pressed [I added a toast to each in the meantime and the line for each is never reached) – Droidthusiast Dec 02 '16 at 21:24