Skip to content

Commit 6136cbd

Browse files
author
Emmanuel Garcia
authored
Give FlutterView a view ID (flutter#27052)
1 parent 2e86f4b commit 6136cbd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ public class FlutterActivity extends Activity
209209
implements FlutterActivityAndFragmentDelegate.Host, LifecycleOwner {
210210
private static final String TAG = "FlutterActivity";
211211

212+
/**
213+
* The ID of the {@code FlutterView} created by this activity.
214+
*
215+
* <p>This ID can be used to lookup {@code FlutterView} in the Android view hierarchy. For more,
216+
* see {@link android.view.View#findViewById}.
217+
*/
218+
public static final int FLUTTER_VIEW_ID = 0xF1F2;
219+
212220
/**
213221
* Creates an {@link Intent} that launches a {@code FlutterActivity}, which creates a {@link
214222
* FlutterEngine} that executes a {@code main()} Dart entrypoint, and displays the "/" route as
@@ -424,7 +432,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
424432
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
425433

426434
configureWindowForTransparency();
427-
setContentView(createFlutterView());
435+
436+
View flutterView = createFlutterView();
437+
flutterView.setId(FLUTTER_VIEW_ID);
438+
setContentView(flutterView);
439+
428440
configureStatusBarForFullscreenFlutterExperience();
429441
}
430442

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public void tearDown() {
6868
FlutterInjector.reset();
6969
}
7070

71+
@Test
72+
public void flutterViewHasId() {
73+
Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application);
74+
ActivityController<FlutterActivity> activityController =
75+
Robolectric.buildActivity(FlutterActivity.class, intent);
76+
FlutterActivity activity = activityController.get();
77+
78+
activity.onCreate(null);
79+
assertNotNull(activity.findViewById(FlutterActivity.FLUTTER_VIEW_ID));
80+
}
81+
7182
@Test
7283
public void itCreatesDefaultIntentWithExpectedDefaults() {
7384
Intent intent = FlutterActivity.createDefaultIntent(RuntimeEnvironment.application);

0 commit comments

Comments
 (0)