29

I am using an AppIntro library in my app.

It has 3 slides. I want to ask the user something when the third slide is shown. To achieve that I am using material dialogs by afollestad.

My code in the AppIntro Activity looks like that:

@Override
    public void onNextPressed() {
        if(this.pager.getCurrentItem() == 2) {
            MaterialDialog dialog = new MaterialDialog.Builder(getApplicationContext())
                    .title("QR Code scannen")
                    .content("Möchtest du den QR Code scannen oder selbst eingeben?")
                    .positiveText("eingeben")
                    .negativeText("scannen")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            Intent intent = new Intent(getApplicationContext(), RegistrationActivity.class);
                            startActivity(intent);
                        }
                    })
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            // TODO
                        }
                    })
                    .show();
        }
    } 

Running the App I get the following issue when I slide to the third slide:

com.afollestad.materialdialogs.MaterialDialog$DialogException: 
Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.
at com.afollestad.materialdialogs.MaterialDialog.show(MaterialDialog.java:1328)
at com.afollestad.materialdialogs.MaterialDialog$Builder.show(MaterialDialog.java:1317)
at com.ads.adstimer.fragment.Registration.RegistrationActivity.onNextPressed(RegistrationActivity.java:64)
at com.github.paolorotolo.appintro.AppIntro$2.onClick(AppIntro.java:118)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I do not really understand the problem. Because the AppIntro Activity is already loaded and I expect it to show a dialog when a specific fragment is shown.

How can I achieve to show a dialog when a specific slide is shown?

Yasin Kaçmaz
  • 5,867
  • 4
  • 34
  • 54
jublikon
  • 3,158
  • 10
  • 35
  • 73

5 Answers5

57
MaterialDialog dialog = new MaterialDialog.Builder(getApplicationContext())

I suppose you need to pass the Activity instance here, not the application context.

aga
  • 25,984
  • 9
  • 77
  • 115
  • 14
    For future readers, simply use this `YourActivityClassName.this`instead of `getApplicationContext()` – K DawG Dec 04 '16 at 18:57
  • 1
    What is the difference? – Srujan Barai Feb 23 '17 at 16:14
  • 2
    The one who keeps the context of the UI is Activity (through YourActivityClassName.this). The getApplicationContext() only keep the context of the application. The dialog needs the UI context, thus we pass the Activity context. – Kharda Mar 24 '17 at 09:31
  • what about if you using adapter then in your bindholder you call materialDialog. i got same error. – Ronel Gonzales Sep 14 '17 at 02:05
  • Then it's meaningless that this library wants to receive `Context` as param in its builder. It should have been `Activity`. – Ugurcan Yildirim Nov 15 '17 at 21:34
13

This can be resolved by confirming that current Activity hasWindowFocus because when you get the context in the fragment, instead of exactly which Activity it is, it might not have windowFocus :

if (((Activity) mContext).hasWindowFocus()) {
        mDialog.show();
}
blueware
  • 4,309
  • 1
  • 32
  • 59
  • thanks man , only solution worked for me is this one , but when do we get window focus , i mean i need to show that dialog some time soon right? – Zulqurnain Jutt Apr 01 '18 at 00:09
  • @ZulqurnainJutt, Checking current activity window focus is necessary during showing/hiding views that pausing/resuming current activity. And yes, you need to show the dialog at some point. Glad that I helped. – blueware Apr 01 '18 at 08:30
10

use

 MaterialDialog dialog = new MaterialDialog.Builder(YourActivityName.class)

do not use getApplicationContext().

Rashid
  • 492
  • 8
  • 19
4

Use "this" instead of "getApplicationContext()" if this error happening even inside the activity.

    mProgress = new MaterialDialog.Builder(this)
Muneef M
  • 1,004
  • 14
  • 15
1
public class MainActivity extend Activity{

    MaterialDialog dialog;

    protected void onCreate(Bundle savedInstanceState) {

        dialog =  new MaterialDialog.Builder(OrderInfoActivity.this)
                .title("Reject?")
                .content("Are you reject this order?")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        Toast.makeText(OrderInfoActivity.this, "Option1", Toast.LENGTH_SHORT).show();
                        }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        Toast.makeText(OrderInfoActivity.this, "Option 2", Toast.LENGTH_SHORT).show();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        Toast.makeText(OrderInfoActivity.this, "Option 3", Toast.LENGTH_SHORT).show();
                    }
                }).build();

        accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                  dialog.show();
            }
        });
    }
}

Here is what i'm using right now, and it's working

Hope it helps :)

Jason Roman
  • 7,503
  • 10
  • 32
  • 35
Anonymous-E
  • 781
  • 9
  • 25