private Window performDestroy(LocalActivityRecord r, boolean finish) {
   Window win;
   win = r.window;
   if (r.curState == RESUMED && !finish) {
     performPause(r, finish);
   }
   if (localLOGV) Log.v(TAG, r.id + ": destroying");
   mActivityThread.performDestroyActivity(r, finish);
   r.activity = null;
   r.window = null;
   if (finish) {
     r.instanceState = null;
   }
   r.curState = DESTROYED;
   return win;
 }
  private void moveToState(LocalActivityRecord r, int desiredState) {
    if (r.curState == RESTORED || r.curState == DESTROYED) {
      // startActivity() has not yet been called, so nothing to do.
      return;
    }

    if (r.curState == INITIALIZING) {
      // Get the lastNonConfigurationInstance for the activity
      HashMap<String, Object> lastNonConfigurationInstances =
          mParent.getLastNonConfigurationChildInstances();
      Object instanceObj = null;
      if (lastNonConfigurationInstances != null) {
        instanceObj = lastNonConfigurationInstances.get(r.id);
      }
      Activity.NonConfigurationInstances instance = null;
      if (instanceObj != null) {
        instance = new Activity.NonConfigurationInstances();
        instance.activity = instanceObj;
      }

      // We need to have always created the activity.
      if (localLOGV) Log.v(TAG, r.id + ": starting " + r.intent);
      if (r.activityInfo == null) {
        r.activityInfo = mActivityThread.resolveActivityInfo(r.intent);
      }
      r.activity =
          mActivityThread.startActivityNow(
              mParent, r.id, r.intent, r.activityInfo, r, r.instanceState, instance);
      if (r.activity == null) {
        return;
      }
      r.window = r.activity.getWindow();
      r.instanceState = null;
      r.curState = STARTED;

      if (desiredState == RESUMED) {
        if (localLOGV) Log.v(TAG, r.id + ": resuming");
        mActivityThread.performResumeActivity(r, true);
        r.curState = RESUMED;
      }

      // Don't do anything more here.  There is an important case:
      // if this is being done as part of onCreate() of the group, then
      // the launching of the activity gets its state a little ahead
      // of our own (it is now STARTED, while we are only CREATED).
      // If we just leave things as-is, we'll deal with it as the
      // group's state catches up.
      return;
    }

    switch (r.curState) {
      case CREATED:
        if (desiredState == STARTED) {
          if (localLOGV) Log.v(TAG, r.id + ": restarting");
          mActivityThread.performRestartActivity(r);
          r.curState = STARTED;
        }
        if (desiredState == RESUMED) {
          if (localLOGV) Log.v(TAG, r.id + ": restarting and resuming");
          mActivityThread.performRestartActivity(r);
          mActivityThread.performResumeActivity(r, true);
          r.curState = RESUMED;
        }
        return;

      case STARTED:
        if (desiredState == RESUMED) {
          // Need to resume it...
          if (localLOGV) Log.v(TAG, r.id + ": resuming");
          mActivityThread.performResumeActivity(r, true);
          r.instanceState = null;
          r.curState = RESUMED;
        }
        if (desiredState == CREATED) {
          if (localLOGV) Log.v(TAG, r.id + ": stopping");
          mActivityThread.performStopActivity(r, false);
          r.curState = CREATED;
        }
        return;

      case RESUMED:
        if (desiredState == STARTED) {
          if (localLOGV) Log.v(TAG, r.id + ": pausing");
          performPause(r, mFinishing);
          r.curState = STARTED;
        }
        if (desiredState == CREATED) {
          if (localLOGV) Log.v(TAG, r.id + ": pausing");
          performPause(r, mFinishing);
          if (localLOGV) Log.v(TAG, r.id + ": stopping");
          mActivityThread.performStopActivity(r, false);
          r.curState = CREATED;
        }
        return;
    }
  }