title: Dismiss Android PopupWindow when switching to a new Tab in TabActivity?
body: <p>I have a MapActivity as one of four tabs in a TabActivity.  This MapActivity can launch a PopupWindow that is a legend.  The PopupWindow remains on the screen, on top of the map, until the "Show Legend" button is clicked again (back and forth, etc.).</p>
  
  <p>The problem is that, when a user switches to another tab, the PopupWindow remains persistent over the view.</p>
  
  <p>I've tried implementing the onPause() method in the MapActivity class, and dismissing it from there.  The application force closes with this method in place.</p>
  
  <p>Any help?  Thanks!</p>
  
  <p>EDIT: Here's some of my code:</p>
  
  <p>In the MainActivity, which establishes the four tabs:</p>
  
  <pre><code>    Resources res = getResources(); // Resource object to get Drawables
      TabHost tabHost = getTabHost();  // The activity TabHost
      TabHost.TabSpec spec;  // Reusable TabSpec for each tab
      Intent intent;  // Reusable Intent for each tab
  
      // Create an Intent to launch an Activity for the tab (to be reused)
      intent = new Intent().setClass(this, FirstActivity.class);
  
      // Initialize a TabSpec for each tab and add it to the TabHost
      spec = tabHost.newTabSpec("game").setIndicator("First",
                        res.getDrawable(R.drawable.ic_tab_game))
                    .setContent(intent);
      tabHost.addTab(spec);
  
      // Do the same for the other tabs
      intent = new Intent().setClass(this, SecondActivity.class);
      spec = tabHost.newTabSpec("alerts").setIndicator("Second",
                        res.getDrawable(R.drawable.ic_tab_alert))
                    .setContent(intent);
      tabHost.addTab(spec);
  
      intent = new Intent().setClass(this, MapActivity.class);
      spec = tabHost.newTabSpec("map").setIndicator("Map",
                        res.getDrawable(R.drawable.ic_tab_map))
                    .setContent(intent);
      tabHost.addTab(spec);
  
      intent = new Intent().setClass(this, LastActivity.class);
      spec = tabHost.newTabSpec("experience").setIndicator("Last",
                        res.getDrawable(R.drawable.ic_tab_experience))
                    .setContent(intent);
      tabHost.addTab(spec);
  
      tabHost.setCurrentTab(0);
  </code></pre>
  
  <p>Now in my MapActivity class (which extends MapActivity):</p>
  
  <pre><code>    // Declare the Legend PopupWindow
      mapLegendInflater = (LayoutInflater) MapActivity.this
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mapLegendPopupLayout = mapLegendInflater.inflate(
              R.layout.maptablegendpopuplayout, null, false);
      mapLegendPopup = new PopupWindow(mapLegendPopupLayout,
              (int) (0.45 * getApplicationContext().getResources()
                      .getDisplayMetrics().widthPixels),
              (int) (0.33 * getApplicationContext().getResources()
                      .getDisplayMetrics().heightPixels), true);
      mapLegendPopup.setFocusable(false);
      mapLegendPopup.setOutsideTouchable(true);
  
              Boolean legendIsShown = false;
  
      mapLegendButton = (Button) findViewById(R.id.buttonMapLegend);
      mapLegendButton.setOnClickListener(mapLegendListener);
  
  
  private OnClickListener mapLegendListener = new OnClickListener() {
      public void onClick(View v) {
          // Launch or dismiss the map legend popup
          if (legendIsShown) {
              mapLegendPopup.dismiss();
              mapLegendButton.getBackground().clearColorFilter();
              legendIsShown = false;
          } else {
              mapLegendPopup.showAtLocation(
                      findViewById(R.id.buttonMapLegend), Gravity.TOP
                              | Gravity.LEFT, 8,
                      (int) (0.23 * getApplicationContext().getResources()
                              .getDisplayMetrics().heightPixels));
              mapLegendButton.getBackground().setColorFilter(
                      new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
              // mapLegendButton.getBackground().setColorFilter(0xFFFFFF00,
              // PorterDuff.Mode.MULTIPLY);
              legendIsShown = true;
          }
      }
  };
  </code></pre>
  
  <p>I hope this provides an idea of where I'm at.  Everything works perfectly well on the Map tab.  It's only when you have the Legend shown and switch tabs that it is still displayed on other views.</p>