Simplify OnContextAvailableListener interface

Rather than pass the ContextAware object,
the Context, and a Bundle for saved instance
state, pass only the Context to the
onContextAvailable() callback.

The ContextAware parameter is not needed in
general (unregistering is a corner case that
can be handled by capturing a reference to the
ContextAware).

The saved instance state Bundle is not needed
when we can ensure that the
SavedStateRegistry has had its state restored
before the listeners are called. This ensures
that all Saved State logic is centralized in
the API specifically built for saving state
rather than leaking into other APIs.

To accomplish this API change, the storage of
FragmentManager's and AppCompatDelegate's saved
state was moved to SavedStateRegistry as part of
FragmentActivity and AppCompatActivity,
respectively. Longer term, FragmentManager and
AppCompatDelegate should provide public APIs for
connecting directly to SavedStateRegistry, but
that is out of scope for this change.

Test: activity, fragment, and appcompat tests pass
BUG: 161390636
Relnote: N/A
Change-Id: I32fdd4b2c4c391c3fa97158dbd7508fa96b08d7a
diff --git a/fragment/fragment/src/main/java/androidx/fragment/app/FragmentActivity.java b/fragment/fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
index 0c1a46a..dbb63d7 100644
--- a/fragment/fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
+++ b/fragment/fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
@@ -38,7 +38,6 @@
 import androidx.activity.ComponentActivity;
 import androidx.activity.OnBackPressedDispatcher;
 import androidx.activity.OnBackPressedDispatcherOwner;
-import androidx.activity.contextaware.ContextAware;
 import androidx.activity.contextaware.OnContextAvailableListener;
 import androidx.activity.result.ActivityResultCallback;
 import androidx.activity.result.ActivityResultRegistry;
@@ -59,6 +58,7 @@
 import androidx.lifecycle.ViewModelStore;
 import androidx.lifecycle.ViewModelStoreOwner;
 import androidx.loader.app.LoaderManager;
+import androidx.savedstate.SavedStateRegistry;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -121,11 +121,28 @@
     }
 
     private void init() {
+        // TODO: Directly connect FragmentManager to SavedStateRegistry
+        getSavedStateRegistry().registerSavedStateProvider(FRAGMENTS_TAG,
+                new SavedStateRegistry.SavedStateProvider() {
+                    @NonNull
+                    @Override
+                    public Bundle saveState() {
+                        Bundle outState = new Bundle();
+                        markFragmentsCreated();
+                        mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
+                        Parcelable p = mFragments.saveAllState();
+                        if (p != null) {
+                            outState.putParcelable(FRAGMENTS_TAG, p);
+                        }
+                        return outState;
+                    }
+                });
         addOnContextAvailableListener(new OnContextAvailableListener() {
             @Override
-            public void onContextAvailable(@NonNull ContextAware contextAware,
-                    @NonNull Context context, @Nullable Bundle savedInstanceState) {
+            public void onContextAvailable(@NonNull Context context) {
                 mFragments.attachHost(null /*parent*/);
+                Bundle savedInstanceState = getSavedStateRegistry()
+                        .consumeRestoredStateForKey(FRAGMENTS_TAG);
 
                 if (savedInstanceState != null) {
                     Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
@@ -443,20 +460,6 @@
     }
 
     /**
-     * Save all appropriate fragment state.
-     */
-    @Override
-    protected void onSaveInstanceState(@NonNull Bundle outState) {
-        super.onSaveInstanceState(outState);
-        markFragmentsCreated();
-        mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
-        Parcelable p = mFragments.saveAllState();
-        if (p != null) {
-            outState.putParcelable(FRAGMENTS_TAG, p);
-        }
-    }
-
-    /**
      * Dispatch onStart() to all fragments.
      */
     @Override
@@ -782,7 +785,7 @@
         }
     }
 
-    private void markFragmentsCreated() {
+    void markFragmentsCreated() {
         boolean reiterate;
         do {
             reiterate = markState(getSupportFragmentManager(), Lifecycle.State.CREATED);