alertDialogBuilder crashes only when relaunching the APP

88 views Asked by At

I have a "alertDialogBuilder" to rename an entry when a button is pressed. This works fine when the App is freshly opened. But If I press the back button (meaning the App is minimized and I am back at the Android home-screen), when I relaunch the App and press the button, this time the APP crashes. This happens every time and I have no idea how to debug this. I have checked the lifecycle and "onPause" and "onStop" are called when pressing the back button. But I don't see why that should be a problem.

Any ideas?

Here is the code where I launch the prompt dialogue in a helper class:

public void loadPromptInput(Context promptcontext, final OnOkGetText onOk, String InitialTxt) {

    //pathText.setText("Prompt input");
    LayoutInflater li = LayoutInflater.from(promptcontext);
    View promptsView = li.inflate(R.layout.prompts_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( promptcontext);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView
            .findViewById(R.id.editTextDialogUserInput);
    userInput.setText("");
    userInput.append(InitialTxt);
      alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            onOk.hereIsYouText(userInput.getText().toString());
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

    // make the keyboard shown by default
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

E/AndroidRuntime: FATAL EXCEPTION: main Process: com...., PID: 31622 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@423c9940 is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:532) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at com....myUtils.loadPromptInput(myUtils.java:71) at com....MainActivity$6.onReceive(MainActivity.java:557) at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:308) at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46) at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:118) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at com.zte.heartyservice.SCC.FrameworkBridge.main(FrameworkBridge.java:136) at dalvik.system.NativeStart.main(Native Meth

od)

1

There are 1 answers

3
Roberto Martucci On BEST ANSWER

It would be helpful looking at your code to see where you call:

void loadPromptInput(Context promptcontext...

...most probably you are passing as parameter an instance of a context no more valid.

In any case before calling your method check if the activity is finishing:

//in a fragment
if(getActivity() != null && !getActivity().isFinishing()) {
    loadPromptInput(getActivity()...
}

//in an activity
if(!isFinishing()) {
    loadPromptInput(this...
}