Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ FlutterFragment retrieveExistingFlutterFragmentIfPossible() {
* 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.
Expand Down Expand Up @@ -602,7 +608,7 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) {
*/
@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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ public void itRetrievesExistingFlutterFragmentWhenRecreated() {
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;

Expand Down