1212import android .content .Intent ;
1313import android .os .Build ;
1414import android .os .Bundle ;
15+ import android .os .Handler ;
1516import android .support .annotation .NonNull ;
1617import android .support .annotation .Nullable ;
1718import android .support .annotation .VisibleForTesting ;
@@ -237,14 +238,8 @@ void onAttach(@NonNull Context context) {
237238 *
238239 * <p>{@code inflater} and {@code container} may be null when invoked from an {@code Activity}.
239240 *
240- * <p>This method:
241- *
242- * <ol>
243- * <li>creates a new {@link FlutterView} in a {@code View} hierarchy
244- * <li>adds a {@link FlutterUiDisplayListener} to it
245- * <li>attaches a {@link FlutterEngine} to the new {@link FlutterView}
246- * <li>returns the new {@code View} hierarchy
247- * </ol>
241+ * <p>This method creates a new {@link FlutterView}, adds a {@link FlutterUiDisplayListener} to
242+ * it, and then returns it.
248243 */
249244 @ NonNull
250245 View onCreateView (
@@ -266,9 +261,6 @@ View onCreateView(
266261 }
267262 flutterSplashView .displayFlutterViewWithSplash (flutterView , host .provideSplashScreen ());
268263
269- Log .v (TAG , "Attaching FlutterEngine to FlutterView." );
270- flutterView .attachToFlutterEngine (flutterEngine );
271-
272264 return flutterSplashView ;
273265 }
274266
@@ -289,13 +281,31 @@ void onActivityCreated(@Nullable Bundle bundle) {
289281 * <p>
290282 *
291283 * <ol>
284+ * <li>Attaches the {@link FlutterEngine} owned by this delegate to the {@link FlutterView}
285+ * owned by this delegate.
292286 * <li>Begins executing Dart code, if it is not already executing.
293287 * </ol>
294288 */
295289 void onStart () {
296290 Log .v (TAG , "onStart()" );
297291 ensureAlive ();
298- doInitialFlutterViewRun ();
292+
293+ // We post() the code that attaches the FlutterEngine to our FlutterView because there is
294+ // some kind of blocking logic on the native side when the surface is connected. That lag
295+ // causes launching Activitys to wait a second or two before launching. By post()'ing this
296+ // behavior we are able to move this blocking logic to after the Activity's launch.
297+ // TODO(mattcarroll): figure out how to avoid blocking the MAIN thread when connecting a surface
298+ new Handler ()
299+ .post (
300+ new Runnable () {
301+ @ Override
302+ public void run () {
303+ Log .v (TAG , "Attaching FlutterEngine to FlutterView." );
304+ flutterView .attachToFlutterEngine (flutterEngine );
305+
306+ doInitialFlutterViewRun ();
307+ }
308+ });
299309 }
300310
301311 /**
@@ -408,6 +418,7 @@ void onStop() {
408418 Log .v (TAG , "onStop()" );
409419 ensureAlive ();
410420 flutterEngine .getLifecycleChannel ().appIsPaused ();
421+ flutterView .detachFromFlutterEngine ();
411422 }
412423
413424 /**
@@ -418,8 +429,6 @@ void onStop() {
418429 void onDestroyView () {
419430 Log .v (TAG , "onDestroyView()" );
420431 ensureAlive ();
421-
422- flutterView .detachFromFlutterEngine ();
423432 flutterView .removeOnFirstFrameRenderedListener (flutterUiDisplayListener );
424433 }
425434
0 commit comments