5

I'm creating an alert dialog using AlertDialog.Builder from onClick callback for a button

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(false);
    builder.setNegativeButton(R.string.cancel, (dialog, which) -> isReporting = false);
    builder.show();

99.9% of the time everything works fine, but recently I started seeing some crash reports like this:

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@db362a2 has already been added
   at android.view.ViewRootImpl.setView(ViewRootImpl.java:902)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
   at android.app.Dialog.show(Dialog.java:538)
   at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:958)
   at com.gurushots.app.adapters.VoteAdapter.showReporting(VoteAdapter.java:242)
   at com.gurushots.app.adapters.VoteAdapter.lambda$onBindViewHolder$2$VoteAdapter(VoteAdapter.java:197)
   at com.gurushots.app.adapters.VoteAdapter$$Lambda$2.onClick(Unknown Source)
   at android.view.View.performClick(View.java:6213)
   at android.view.View$PerformClick.run(View.java:23645)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6642)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

exception indicates I'm trying to re-add alert that already shown, but it's not the case. why this happens?

dudiktsah
  • 61
  • 1
  • 7
  • [This](https://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window) thread expaning the same .Please have a look . – ADM Nov 12 '17 at 14:16
  • 4
    it's not the same exception and not the same reason – dudiktsah Nov 13 '17 at 15:17
  • could you manage to fix this ? – cgr Nov 27 '17 at 15:54
  • nope. I see recently many exception and crashes that indicates the app is in the background(like get activity method return null) even though the starting point of my code in the stack trace is onClick callback. it doesn't make sense that user click a view when the app not in the front – dudiktsah Nov 29 '17 at 07:50

0 Answers0