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 2 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 @@ -241,6 +241,10 @@ public Intent build(@NonNull Context context) {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
switchLaunchThemeForNormalTheme();
// Get an existing fragment reference first before onCreate since onCreate would re-attach
// existing fragments. This would cause FlutterFragment to reference the host activity which
// should be aware of its child fragment.
flutterFragment = retrieveExistingFlutterFragmentIfPossible();

super.onCreate(savedInstanceState);

Expand Down Expand Up @@ -366,21 +370,31 @@ private View createFragmentContainer() {
return container;
}

/**
* Retrieves the previously created {@link FlutterFragment} if possible.
*
* <p>If the activity is recreated, an existing {@link FlutterFragment} may already exist. Retain
* a reference to that {@link FlutterFragment} in the {@code #flutterFragment} field and avoid
* re-creating another {@link FlutterFragment}.
*/
@VisibleForTesting
FlutterFragment retrieveExistingFlutterFragmentIfPossible() {
FragmentManager fragmentManager = getSupportFragmentManager();
return (FlutterFragment) fragmentManager.findFragmentByTag(TAG_FLUTTER_FRAGMENT);
}

/**
* Ensure that a {@link FlutterFragment} is attached to this {@code FlutterFragmentActivity}.
*
* <p>If no {@link FlutterFragment} exists in this {@code FlutterFragmentActivity}, then a {@link
* FlutterFragment} is created and added. If a {@link FlutterFragment} does exist in this {@code
* FlutterFragmentActivity}, then a reference to that {@link FlutterFragment} is retained in
* {@code #flutterFragment}.
* FlutterFragment} is created and added.
*/
private void ensureFlutterFragmentCreated() {
FragmentManager fragmentManager = getSupportFragmentManager();
flutterFragment = (FlutterFragment) fragmentManager.findFragmentByTag(TAG_FLUTTER_FRAGMENT);
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.
flutterFragment = createFlutterFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.add(FRAGMENT_CONTAINER_ID, flutterFragment, TAG_FLUTTER_FRAGMENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.HANDLE_DEEPLINKING_META_DATA_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -170,7 +171,37 @@ public void itAllowsRootLayoutOverride() {
assertTrue(foundCustomView);
}

@Test
public void itCreatesAValidFlutterFragment() {
FlutterFragmentActivityWithProvidedEngine activity =
Robolectric.buildActivity(FlutterFragmentActivityWithProvidedEngine.class).get();

// Creating the FlutterFragmentActivity will create and attach the FlutterFragment, causing
// a FlutterEngine to be created.
activity.onCreate(null);
assertNotNull(activity.getFlutterEngine());
assertEquals(1, activity.engineCreated);
}

@Test
public void itRetrievesExistingFlutterFragmentWhenRecreated() {
FlutterFragmentActivityWithProvidedEngine activity =
spy(Robolectric.buildActivity(FlutterFragmentActivityWithProvidedEngine.class).get());

FlutterFragment fragment = mock(FlutterFragment.class);
when(activity.retrieveExistingFlutterFragmentIfPossible()).thenReturn(fragment);

FlutterEngine engine = mock(FlutterEngine.class);
when(fragment.getFlutterEngine()).thenReturn(engine);

activity.onCreate(null);
assertEquals(engine, activity.getFlutterEngine());
assertEquals(0, activity.engineCreated);
}

static class FlutterFragmentActivityWithProvidedEngine extends FlutterFragmentActivity {
int engineCreated = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/engineCreated/numberOfEnginesCreated


@Override
protected FlutterFragment createFlutterFragment() {
return FlutterFragment.createDefault();
Expand All @@ -184,6 +215,7 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) {
when(flutterJNI.isAttached()).thenReturn(true);
when(flutterLoader.automaticallyRegisterPlugins()).thenReturn(true);

engineCreated++;
return new FlutterEngine(context, flutterLoader, flutterJNI, new String[] {}, true);
}
}
Expand Down