-1

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)

Vahid
  • 13
  • 4
  • please post your code, where have you placed your alert dialog? – dhami_ji Dec 07 '17 at 17:02
  • Just added the code. It is actually a user input prompt dialog whose code is written inside a helper class (myUtils.java). It is called, when the rename option is selected from a menu (that is inflated from within an Adapter class for a recyclerview). – Vahid Dec 07 '17 at 17:16

1 Answers1

0

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...
}
Roberto Martucci
  • 1,237
  • 1
  • 13
  • 21
  • Thanks for your answer. But, if the activity !isFinishing() is false, then it will not be launched. But it should... And my confusion is why is this happening when I relaunch the App (after backpress). I am calling this code in a complicated manner. An intent is sent after a menu_item is selected in an adaptervirew class, and the intent is filtered in the mainactivity fot the "edit" command. That's how I could make the adapter (for recyclerview) that launches the menu layout, to speak with the Mainactivity that contain the recyclerview. – Vahid Dec 07 '17 at 17:42
  • Which context are you using then? I guess that is the problem – Roberto Martucci Dec 07 '17 at 18:06
  • 1
    Thanks, I think you pointed me to the solution. The issue was that I was using an intent and was not unregistering it. So, when launching again after BackPress, the destroyed activity was still listening to it (this is my understanding) and trying to launch a dialog from the dead context. I am accepting your answer, hoping that people would read the comments. – Vahid Dec 07 '17 at 18:54