title: BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@cb70704 has already been added On Samsung devices
body: <p>I'm using Xamarin for Android and I'm getting this error in an AlertDialog.Builder.Show() method, but it only happens on some Samsung (with Android 7.0) devices, we have tried some other devices and this problem doesn't happen. I'm getting it only after using the application some time. The stacktrace is the following</p>
  
  <pre><code>  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in &lt;f32579baafc1404fa37ba3ec1abdc0bd&gt;:0 
    at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in &lt;7802aa64ad574c33adca332a3fa9706a&gt;:0 
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in &lt;7802aa64ad574c33adca332a3fa9706a&gt;:0 
    at Android.App.AlertDialog+Builder.Show () [0x0000a] in &lt;dc51acef1f304f0dab449a7fc6039799&gt;:0 
    at Prizma.Controls.Common.BindingComboBox.ShowDialog () [0x00062] in C:\TeamProjects\PrizmaProject\Main\MobileSales.iOS\Prizma.Controls.iOS\Common\BindingComboBox.cs:408 
    --- End of managed Android.Views.WindowManagerBadTokenException stack trace ---
  android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@cb70704 has already been added
      at android.view.ViewRootImpl.setView(ViewRootImpl.java:902)
      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
      at android.app.Dialog.show(Dialog.java:404)
      at android.app.AlertDialog$Builder.show(AlertDialog.java:1136)
      at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
      at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
      at android.view.View.performClick(View.java:6261)
      at android.widget.TextView.performClick(TextView.java:11185)
      at android.view.View$PerformClick.run(View.java:23752)
      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:6776)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
  </code></pre>
  
  <p>My code is something like this</p>
  
  <pre><code>            try
              {
                  AlertDialog.Builder a = new AlertDialog.Builder(MainActivity);
                  a.SetTitle("Select");
                  a.SetAdapter(_Adapter, new EventHandler&lt;DialogClickEventArgs&gt;(ClosedDialog));
                  a.Create();
                  a.Show();
              }
              catch (Exception ex)
              {
                  //Exception code
              }
  </code></pre>
  
  <p>I put the AlertDialog.Builder Show() method in a Try Catch, but after I get the exception for the first time, I keep getting it everytime.</p>
  
  <p>Some remarks</p>
  
  <ul>
  <li>I'm still getting this error even the application never goes to the background.</li>
  <li>The app is compiled using the latest Android SDK (8.1)</li>
  <li>I didin't have Xamarin Android to the latest version, but now I have it and the problem is still there.</li>
  <li>Not just the AlertDialogs doesn't show after the error, the Popup menus too.</li>
  <li>I got a Samsung Galaxy J7 Prime with Android 6.0.1 and this error didn't happen, but after upgrade it to 7.0 the problem started. In other devices like Motorola, Hawuei, LG, etc.. we don't have this issue even they have Android 7.0.</li>
  </ul>
  
  <p>Please help me, I have weeks with this issue and my clients that have Samsung devices are killing me :)</p>
  
  <p>Thanks in advance.</p>
  
  <p>Regards</p>
  
  <p>Allan</p>