Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit eadb540

Browse files
authored
Made sure we get the initial route from the intent. (#40148)
Android: Made sure we get the initial route from the intent.
1 parent 7893f10 commit eadb540

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,16 @@ private FlutterEngineGroup.Options addEntrypointOptions(FlutterEngineGroup.Optio
235235
DartExecutor.DartEntrypoint dartEntrypoint =
236236
new DartExecutor.DartEntrypoint(
237237
appBundlePathOverride, host.getDartEntrypointFunctionName());
238+
String initialRoute = host.getInitialRoute();
239+
if (initialRoute == null) {
240+
initialRoute = maybeGetInitialRouteFromIntent(host.getActivity().getIntent());
241+
if (initialRoute == null) {
242+
initialRoute = DEFAULT_INITIAL_ROUTE;
243+
}
244+
}
238245
return options
239246
.setDartEntrypoint(dartEntrypoint)
240-
.setInitialRoute(host.getInitialRoute())
247+
.setInitialRoute(initialRoute)
241248
.setDartEntrypointArgs(host.getDartEntrypointArgs());
242249
}
243250

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,47 @@ public void itThrowsExceptionIfCachedEngineDoesNotExist() {
237237
// Expect IllegalStateException.
238238
}
239239

240+
// Bug: b/271100292
241+
@Test
242+
public void flutterEngineGroupGetsInitialRouteFromIntent() {
243+
// ---- Test setup ----
244+
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
245+
Activity mockActivity = mock(Activity.class);
246+
Intent mockIntent = mock(Intent.class);
247+
when(mockFlutterLoader.findAppBundlePath()).thenReturn("default_flutter_assets/path");
248+
FlutterInjector.setInstance(
249+
new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
250+
FlutterEngineGroup flutterEngineGroup = mock(FlutterEngineGroup.class);
251+
FlutterEngineGroupCache.getInstance().put("my_flutter_engine_group", flutterEngineGroup);
252+
253+
List<String> entryPointArgs = new ArrayList<>();
254+
entryPointArgs.add("entrypoint-arg");
255+
256+
// Adjust fake host to request cached engine group.
257+
when(mockHost.getInitialRoute()).thenReturn(null);
258+
when(mockHost.getCachedEngineGroupId()).thenReturn("my_flutter_engine_group");
259+
when(mockHost.provideFlutterEngine(any(Context.class))).thenReturn(null);
260+
when(mockHost.shouldAttachEngineToActivity()).thenReturn(false);
261+
when(mockHost.getDartEntrypointArgs()).thenReturn(entryPointArgs);
262+
when(mockHost.shouldHandleDeeplinking()).thenReturn(true);
263+
when(mockHost.getActivity()).thenReturn(mockActivity);
264+
when(mockActivity.getIntent()).thenReturn(mockIntent);
265+
when(mockIntent.getData()).thenReturn(Uri.parse("foo://example.com/initial_route"));
266+
267+
// Create the real object that we're testing.
268+
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
269+
270+
// --- Execute the behavior under test ---
271+
// The FlutterEngine is obtained in onAttach().
272+
delegate.onAttach(ctx);
273+
274+
DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint("/fake/path", "main");
275+
ArgumentCaptor<FlutterEngineGroup.Options> optionsCaptor =
276+
ArgumentCaptor.forClass(FlutterEngineGroup.Options.class);
277+
verify(flutterEngineGroup, times(1)).createAndRunEngine(optionsCaptor.capture());
278+
assertEquals("/initial_route", optionsCaptor.getValue().getInitialRoute());
279+
}
280+
240281
@Test
241282
public void itUsesNewEngineInGroupWhenProvided() {
242283
// ---- Test setup ----

0 commit comments

Comments
 (0)