title: Android PopupWindow TooManySurfacesException
body: <p>My game windows are all floating PopupWindows</p>
  <pre><code>backpackPopupWindow = new PopupWindow(backpackView, width, height, focusable);
  </code></pre>
  <p>Some of my users reported after 30 minutes or so it will randomly crash with no information in my game's log file.  I was able to get it to reproduce on a friend's Galaxy S8 phone and here is what I see in the log.  First two lines below are from my game's logs (receiving information about the player's backpack from the server).  A google for TooManySurfacesException returns 8 results :(</p>
  <pre><code>2020-07-22 22:45:58.703 27227-27227/com.kisnardonline.kisnardonline I/BitmapStore: BitmapStore: BitmapStore: First load: res/images/item/item_mouse_tail.png
  2020-07-22 22:45:58.734 27227-27525/com.kisnardonline.kisnardonline I/MyCommandReceiver: Promage-backpackWindow@40-37-32|I|Gold|Shiny little coins.|coins.png...
  2020-07-22 22:45:58.737 697-697/? E/SurfaceFlinger: createLayer() failed, already application created very many surfaces [PopupWindow:5659ba6[27227]]
  2020-07-22 22:45:58.739 1411-4256/? E/SurfaceComposerClient: SurfaceComposerClient::createSurface error Broken pipe
  2020-07-22 22:45:58.739 1411-4256/? E/SurfaceControl: @!@! Throw TooManySurfacesException [PopupWindow:5659ba6[27227]]
  2020-07-22 22:45:58.742 1411-4256/? E/WindowManager: A process tried to create too many surfaces
      android.view.WindowManager$TooManySurfacesException
          at android.view.SurfaceControl.nativeCreate(Native Method)
          at android.view.SurfaceControl.&lt;init&gt;(SurfaceControl.java:704)
          at android.view.SurfaceControl.&lt;init&gt;(SurfaceControl.java:72)
          at android.view.SurfaceControl$Builder.build(SurfaceControl.java:456)
          at com.android.server.wm.WindowSurfaceController.&lt;init&gt;(WindowSurfaceController.java:141)
          at com.android.server.wm.WindowStateAnimator.createSurfaceLocked(WindowStateAnimator.java:632)
          at com.android.server.wm.WindowManagerService.createSurfaceControl(WindowManagerService.java:2779)
          at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2465)
          at com.android.server.wm.Session.relayoutForTranslate(Session.java:302)
          at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:518)
          at com.android.server.wm.Session.onTransact(Session.java:186)
          at android.os.Binder.execTransact(Binder.java:739)
  2020-07-22 22:45:58.742 1411-4256/? I/Process: Sending signal. PID: 27227 SIG: 9
  </code></pre>
  <p>The second line where the backpack window information is received will &quot;repaint&quot; or &quot;show&quot; the player's backpack:</p>
  <pre><code>if (GameActivity.askedForBackpack || BackpackPanel.isViewVisible){
      if (BackpackPanel.isViewVisible) {
          GameActivity.backpackPanel.RepaintBackpack();
      } else {
          GameActivity.backpackPanel.ShowPopupBackpack(GameActivity.gameActivityRelativeLayout);
      }
      BackpackPanel.isViewVisible = true;
      GameActivity.askedForBackpack = false;
  }
  </code></pre>
  <p>RepaintBackpack() loops through the data from server and updates the ImageViews (myImage):</p>
  <pre><code>if (BackpackPanel.backpackHolderFilename[ii].endsWith(&quot;.png&quot;)){
      myImage.setImageBitmap(BitmapStore.get().getBitmap(BackpackPanel.backpackHolderFilename[ii]));
  } else if (BackpackPanel.backpackHolderFilename[ii].endsWith(&quot;.gif&quot;)){
      GlideApp.with(MyCommandReceiver.GetActiveActivity())
              .load(new File(Environment.getExternalStorageDirectory() + &quot;/KisnardOnline/&quot; + BackpackPanel.backpackHolderFilename[ii]))
              .override(ScreenUtils.getDPFromPixels(32), ScreenUtils.getDPFromPixels(32))
              .into(myImage);
  } else if (BackpackPanel.backpackHolderFilename[ii].equals(&quot;&quot;)){
      myImage.setImageResource(android.R.color.transparent);
  }
  </code></pre>
  <p>ShowPopupBackpack() creates the backpack PopupWindow and sets click listeners, etc:</p>
  <pre><code>// inflate the layout of the popup window
  backpackView = MyCommandReceiver.GetLayoutInflater().inflate(R.layout.window_backpack, null);
  
  // create the popup window
  int width = LinearLayout.LayoutParams.WRAP_CONTENT;
  int height = LinearLayout.LayoutParams.WRAP_CONTENT;
  boolean focusable = false; // do not let taps outside the popup also dismiss it
  backpackPopupWindow = new PopupWindow(backpackView, width, height, focusable);
  backpackPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
  backpackPopupWindow.setOutsideTouchable(false);
  </code></pre>
  <p>Here is what the game (with backpack showing) looks like:</p>
  <p>[<img src="https://i.stack.imgur.com/9pBHb.png" alt="screenshot of KisnardOnline backpack PopupWindow1" /></p>