title: IllegalStateException of toast View on Android P preview
body: <p>While trying to release my app for production, the pre-launch report notified me of an error on Pixel 2 Android P Preview device. The error is related to a custom toast message I have, saying that a View "has already been added to the window manager":</p>
  
  <pre><code>java.lang.IllegalStateException: View android.support.constraint.ConstraintLayout{efbeb21 V.E...... ......ID 0,0-788,1124 #7f0900db app:id/toast_correct_container} has already been added to the window manager.
  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:328)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
  at android.widget.Toast$TN.handleShow(Toast.java:499)
  at android.widget.Toast$TN$1.handleMessage(Toast.java:403)
  at android.os.Handler.dispatchMessage(Handler.java:106)
  at androidx.test.espresso.base.Interrogator.a(Interrogator.java:19)
  at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:142)
  at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:134)
  at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:34)
  at androidx.test.espresso.action.MotionEvents.a(MotionEvents.java:74)
  at androidx.test.espresso.action.MotionEvents.a(MotionEvents.java:52)
  at androidx.test.espresso.action.Tap.c(Tap.java:9)
  at androidx.test.espresso.action.Tap.a(Tap.java:19)
  at androidx.test.espresso.action.Tap$1.b(Tap.java:2)
  at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:22)
  at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:9)
  at androidx.test.espresso.ViewInteraction.a(ViewInteraction.java:78)
  at androidx.test.espresso.ViewInteraction.a(ViewInteraction.java:94)
  at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:3)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at android.os.Handler.handleCallback(Handler.java:873)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:193)
  at android.app.ActivityThread.main(ActivityThread.java:6669)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  </code></pre>
  
  <p>In MainActivity OnCreate I call this method that inflates the toast View:</p>
  
  <pre><code>private void initToastObjects() {
      mToastCorrect = new Toast(this);
      mToastWrong = new Toast(this);
  
      // inflate view
      LayoutInflater myInflater = LayoutInflater.from(this);
      mLayoutCorrect = myInflater.inflate(R.layout.toast_correct, (ViewGroup) findViewById(R.id.toast_correct_container));
      mLayoutWrong = myInflater.inflate(R.layout.toast_wrong, (ViewGroup) findViewById(R.id.toast_wrong_container));
  }
  </code></pre>
  
  <p>I later set dynamically different images to the toast according to user selection:</p>
  
  <pre><code>mToastCorrect.setView(mLayoutCorrect);
  </code></pre>
  
  <p>I show the toast messages every time the user click correct/wrong answers, and cancel the other toast if it's displayed:</p>
  
  <pre><code>    // cancel previous wrong answer toast and display correct answer toast
      try {
          if (mToastWrong.getView().isShown()) {
              mToastWrong.cancel();
          }
          mToastCorrect.show();
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Any help is appreciated!</p>
  
  <ol>
  <li>How should I fix this issue?</li>
  <li>Why do I get this error only on Android P Preview device?</li>
  <li>If I inflate the View only once during MainActivity OnCreate, how come I get an error saying that the view "has already been added to the window manager"?</li>
  </ol>