More fragment stuff:

- New startActivityForResult() API.
- Fragments now should have the correct lifecycle while hanging around
  in the back stack (not being destroyed and re-created).
- Rework of state save/restore to make it simpler and better.  In theory
  now any fragment (including layout and others in the hierarchy) can
  now be retained across config changes, though this hasn't been tested.

Change-Id: I2a7fe560e14e567f5675a2f172a23fca67b3e97f
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 5b0af67..174c15b 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -16,7 +16,6 @@
 
 package android.app;
 
-import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.HashMap;
 
@@ -3162,6 +3161,36 @@
     }
 
     /**
+     * This is called when a Fragment in this activity calls its 
+     * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
+     * method.
+     * 
+     * <p>This method throws {@link android.content.ActivityNotFoundException}
+     * if there was no Activity found to run the given Intent.
+     * 
+     * @param fragment The fragment making the call.
+     * @param intent The intent to start.
+     * @param requestCode Reply request code.  < 0 if reply is not requested. 
+     * 
+     * @throws android.content.ActivityNotFoundException
+     * 
+     * @see Fragment#startActivity 
+     * @see Fragment#startActivityForResult 
+     */
+    public void startActivityFromFragment(Fragment fragment, Intent intent, 
+            int requestCode) {
+        Instrumentation.ActivityResult ar =
+            mInstrumentation.execStartActivity(
+                this, mMainThread.getApplicationThread(), mToken, fragment,
+                intent, requestCode);
+        if (ar != null) {
+            mMainThread.sendActivityResult(
+                mToken, fragment.mWho, requestCode,
+                ar.getResultCode(), ar.getResultData());
+        }
+    }
+
+    /**
      * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
      * taking a IntentSender; see
      * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
@@ -3433,8 +3462,7 @@
      * @see #createPendingResult
      * @see #setResult(int)
      */
-    protected void onActivityResult(int requestCode, int resultCode,
-            Intent data) {
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     }
 
     /**
@@ -3854,9 +3882,14 @@
                 fragment.mFromLayout = true;
                 fragment.mFragmentId = id;
                 fragment.mTag = tag;
-                fragment.onInflate(this, attrs);
                 mFragments.addFragment(fragment, true);
             }
+            // If this fragment is newly instantiated (either right now, or
+            // from last saved state), then give it the attributes to
+            // initialize itself.
+            if (!fragment.mRetaining) {
+                fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
+            }
             if (fragment.mView == null) {
                 throw new IllegalStateException("Fragment " + fname
                         + " did not create a view.");
@@ -4059,6 +4092,11 @@
             + ", resCode=" + resultCode + ", data=" + data);
         if (who == null) {
             onActivityResult(requestCode, resultCode, data);
+        } else {
+            Fragment frag = mFragments.findFragmentByWho(who);
+            if (frag != null) {
+                frag.onActivityResult(requestCode, resultCode, data);
+            }
         }
     }
 }