Fix crash when both FlutterFragmentActivity and FlutterFragment are destroyed and recreated (flutter/engine#25851)

This commit is contained in:
xster
2021-05-03 09:35:45 -07:00
committed by GitHub
parent bbd0f6d329
commit 7e60885377
2 changed files with 27 additions and 1 deletions

View File

@@ -390,6 +390,12 @@ public class FlutterFragmentActivity extends FragmentActivity
* FlutterFragment} is created and added.
*/
private void ensureFlutterFragmentCreated() {
if (flutterFragment == null) {
// If both activity and fragment have been destroyed, the activity restore may have
// already recreated a new instance of the fragment again via the FragmentActivity.onCreate
// and the FragmentManager.
flutterFragment = retrieveExistingFlutterFragmentIfPossible();
}
if (flutterFragment == null) {
// No FlutterFragment exists yet. This must be the initial Activity creation. We will create
// and add a new FlutterFragment to this Activity.
@@ -602,7 +608,7 @@ public class FlutterFragmentActivity extends FragmentActivity
*/
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
if (flutterFragment.isFlutterEngineInjected()) {
if (flutterFragment != null && flutterFragment.isFlutterEngineInjected()) {
// If the FlutterEngine was explicitly built and injected into this FlutterActivity, the
// builder should explicitly decide whether to automatically register plugins via the
// FlutterEngine's construction parameter or via the AndroidManifest metadata.

View File

@@ -199,6 +199,26 @@ public class FlutterFragmentActivityTest {
assertEquals(0, activity.numberOfEnginesCreated);
}
@Test
public void itHandlesNewFragmentRecreationDuringRestoreWhenActivityIsRecreated() {
FlutterFragmentActivityWithProvidedEngine activity =
spy(Robolectric.buildActivity(FlutterFragmentActivityWithProvidedEngine.class).get());
FlutterFragment fragment = mock(FlutterFragment.class);
// Similar to the above case, except here, it's not just the activity that was destroyed and
// could have its fragment restored in the fragment manager. Here, both activity and fragment
// are destroyed. And the fragment manager recreated the fragment on activity recreate.
when(activity.retrieveExistingFlutterFragmentIfPossible()).thenReturn(null, fragment);
FlutterEngine engine = mock(FlutterEngine.class);
when(fragment.getFlutterEngine()).thenReturn(engine);
activity.onCreate(null);
// The framework would have recreated a new fragment but the fragment activity wouldn't have
// created a new one again.
assertEquals(0, activity.numberOfEnginesCreated);
}
static class FlutterFragmentActivityWithProvidedEngine extends FlutterFragmentActivity {
int numberOfEnginesCreated = 0;