@Override
  public void onResume() {
    super.onResume();

    SessionList sessions = mTermSessions;
    TermViewFlipper viewFlipper = mViewFlipper;
    if (sessions != null) {
      sessions.addCallback(this);
      WindowListAdapter adapter = mWinListAdapter;
      if (adapter != null) {
        sessions.addCallback(adapter);
        sessions.addTitleChangedListener(adapter);
        viewFlipper.addCallback(adapter);
      }
    }
    if (sessions != null && sessions.size() < viewFlipper.getChildCount()) {
      for (int i = 0; i < viewFlipper.getChildCount(); ++i) {
        EmulatorView v = (EmulatorView) viewFlipper.getChildAt(i);
        if (!sessions.contains(v.getTermSession())) {
          v.onPause();
          viewFlipper.removeView(v);
          --i;
        }
      }
    }

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    // the HOME dir needs to be set here since it comes from Context
    SharedPreferences.Editor editor = mPrefs.edit();
    String defValue = getDir("HOME", MODE_PRIVATE).getAbsolutePath();
    String homePath = mPrefs.getString("home_path", defValue);
    editor.putString("home_path", homePath);
    editor.commit();

    mSettings.readPrefs(mPrefs);
    updatePrefs();

    if (onResumeSelectWindow >= 0) {
      viewFlipper.setDisplayedChild(onResumeSelectWindow);
      onResumeSelectWindow = -1;
    }
    viewFlipper.onResume();
  }
  // Called when the list of sessions changes
  public void onUpdate() {
    SessionList sessions = mTermSessions;
    if (sessions == null) {
      return;
    }

    if (sessions.size() == 0) {
      mStopServiceOnFinish = true;
      finish();
    } else if (sessions.size() < mViewFlipper.getChildCount()) {
      for (int i = 0; i < mViewFlipper.getChildCount(); ++i) {
        EmulatorView v = (EmulatorView) mViewFlipper.getChildAt(i);
        if (!sessions.contains(v.getTermSession())) {
          v.onPause();
          mViewFlipper.removeView(v);
          --i;
        }
      }
    }
  }
  private void doCloseWindow() {
    if (mTermSessions == null) {
      return;
    }

    EmulatorView view = getCurrentEmulatorView();
    if (view == null) {
      return;
    }
    TermSession session = mTermSessions.remove(mViewFlipper.getDisplayedChild());
    view.onPause();
    session.finish();
    mViewFlipper.removeView(view);
    if (mTermSessions.size() == 0) {
      mStopServiceOnFinish = true;
      finish();
    } else {
      mViewFlipper.showNext();
    }
  }