0

I m using OKHttp : https://github.com/square/okhttp/wiki/Recipes library to do some request for my Android application and I am using a library to display Dialog Box : https://github.com/afollestad/material-dialogs

Here is a simplify code to explain my problem :

mLoginButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                            try {
                                post(SERVER_ADDRESS + "/login", userEmail, userPassword, new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {

                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {

                                        if (response.isSuccessful()) {
                                            String responseUserConnected = response.body().string();

                                            if (/*user not find error*/) {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Helper.loginError(LoginActivity.this); // CRASH HERE
                                                        mLoginButton.setEnabled(true);
                                                        mSignUpButton.setEnabled(true);
                                                    }
                                                }
                                            }

                                        } 
                                    }
                                });
                            } catch (IOException mE) {
                                mE.printStackTrace();
                            }


                }
            });

And here is the code for my DialogBox method :

 public static void loginError(Context context){
        new AlertDialogWrapper.Builder(context)
                .setCancelable(false)
                .setTitle(R.string.login_error_title)
                .setMessage(R.string.login_error_message)
                .setPositiveButton(R.string.login_error_ok_button_text, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

This working fine in general but I found that when I pause the current activity here it's LoginActivity, or if I open an other app during the request, when the application want to show the dialog box, the application stop and crash, I suppose it's because it can't find the current activity to create my DialogBox. I have this error :

Fatal Exception: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I try to use, getApplication(), getApplicationContext(), getBaseContext() methods, but it showed me this message :

Fatal Exception: com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.

How can I prevent or solved these crashes ?

fandro
  • 4,833
  • 7
  • 41
  • 62

1 Answers1

1

AFAIK you cant show a normal AlertDialog/dialog when the activity is not in the foreground. But there is one option that you can consider to use an activity whose theme is set to that of a dialog. Which will work as a dialog as well as having the same life cycle properties of an Activity.

You cannot create a dialog with getApplication(), getApplicationContext() or getBaseContext() because dialog is owned by an Activity. So you should use Activity.this

Community
  • 1
  • 1
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56