/**
   * Find a MIDlet that wants the foreground. If none wants the foreground then find one that is not
   * paused, if no find one that is paused and wants the foreground, then find one that is paused.
   * Only return the "last MIDlet to have the foreground" if there are not other MIDlets.
   *
   * @return new foreground task or null
   */
  private MIDletProxy findNextForegroundMIDlet() {
    Enumeration midlets;

    // find the first task that is active and wants foreground
    midlets = midletProxyList.getMIDlets();
    while (midlets.hasMoreElements()) {
      MIDletProxy current = (MIDletProxy) midlets.nextElement();

      if (current.getMidletState() != MIDletProxy.MIDLET_ACTIVE) {
        continue;
      }

      if (current.wantsForeground() && current != getLastMidletInForeground()) {
        return current;
      }
    }

    // find the first task that is active
    midlets = midletProxyList.getMIDlets();
    while (midlets.hasMoreElements()) {
      MIDletProxy current = (MIDletProxy) midlets.nextElement();

      if (current.getMidletState() != MIDletProxy.MIDLET_ACTIVE
          && current != getLastMidletInForeground()) {
        return current;
      }
    }

    // find the first task that is paused and wants the foreground
    midlets = midletProxyList.getMIDlets();
    while (midlets.hasMoreElements()) {
      MIDletProxy current = (MIDletProxy) midlets.nextElement();

      if (current.getMidletState() != MIDletProxy.MIDLET_PAUSED
          && current != getLastMidletInForeground()) {
        continue;
      }

      if (current.wantsForeground() && current != getLastMidletInForeground()) {
        return current;
      }
    }

    // find the first task that is paused
    midlets = midletProxyList.getMIDlets();
    while (midlets.hasMoreElements()) {
      MIDletProxy current = (MIDletProxy) midlets.nextElement();

      if (current.getMidletState() != MIDletProxy.MIDLET_PAUSED
          && current != getLastMidletInForeground()) {
        return current;
      }
    }

    return getLastMidletInForeground();
  }
 /** Performs this classes function. */
 public void run() {
     isolate.waitForExit();
     parent.notifyListeners(midlet.getSuiteId(), midlet.getClassName());
 }