Java Code Examples for io.flutter.view.FlutterMain#startInitialization()

The following examples show how to use io.flutter.view.FlutterMain#startInitialization() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Flutter.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link FlutterView} linked to the specified {@link Activity} and {@link Lifecycle}.
 * The optional initial route string will be made available to the Dart code (via
 * {@code window.defaultRouteName}) and may be used to determine which widget should be displayed
 * in the view. The default initialRoute is "/".
 *
 * @param activity an {@link Activity}
 * @param lifecycle a {@link Lifecycle}
 * @param initialRoute an initial route {@link String}, or null
 * @return a {@link FlutterView}
 */
@NonNull
public static FlutterView createView(@NonNull final Activity activity, @NonNull final Lifecycle lifecycle, final String initialRoute) {
  FlutterMain.startInitialization(activity.getApplicationContext());
  FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);
  final FlutterNativeView nativeView = new FlutterNativeView(activity);
  final FlutterView flutterView = new FlutterView(activity, null, nativeView) {
    private final BasicMessageChannel<String> lifecycleMessages = new BasicMessageChannel<>(this, "flutter/lifecycle", StringCodec.INSTANCE);
    @Override
    public void onFirstFrame() {
      super.onFirstFrame();
      setAlpha(1.0f);
    }

    @Override
    public void onPostResume() {
      // Overriding default behavior to avoid dictating system UI via PlatformPlugin.
      lifecycleMessages.send("AppLifecycleState.resumed");
    }
  };
  if (initialRoute != null) {
    flutterView.setInitialRoute(initialRoute);
  }
  lifecycle.addObserver(new LifecycleObserver() {
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
      final FlutterRunArguments arguments = new FlutterRunArguments();
      arguments.bundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
      arguments.entrypoint = "main";
      flutterView.runFromBundle(arguments);
      GeneratedPluginRegistrant.registerWith(flutterView.getPluginRegistry());
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
      flutterView.onStart();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
      flutterView.onPostResume();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
      flutterView.onPause();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
      flutterView.onStop();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
      flutterView.destroy();
    }
  });
  flutterView.setAlpha(0.0f);
  return flutterView;
}
 
Example 2
Source File: MainApplication.java    From reader-sdk-flutter-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
  super.onCreate();
  ReaderSdk.initialize(this);
  FlutterMain.startInitialization(this);
}
 
Example 3
Source File: Flutter.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a {@link FlutterView} linked to the specified {@link Activity} and {@link Lifecycle}.
 * The optional initial route string will be made available to the Dart code (via
 * {@code window.defaultRouteName}) and may be used to determine which widget should be displayed
 * in the view. The default initialRoute is "/".
 *
 * @param activity an {@link Activity}
 * @param lifecycle a {@link Lifecycle}
 * @param initialRoute an initial route {@link String}, or null
 * @return a {@link FlutterView}
 */
@NonNull
public static FlutterView createView(@NonNull final Activity activity, @NonNull final Lifecycle lifecycle, final String initialRoute) {
  FlutterMain.startInitialization(activity.getApplicationContext());
  FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);
  final FlutterNativeView nativeView = new FlutterNativeView(activity);
  final FlutterView flutterView = new FlutterView(activity, null, nativeView) {
    private final BasicMessageChannel<String> lifecycleMessages = new BasicMessageChannel<>(this, "flutter/lifecycle", StringCodec.INSTANCE);
    @Override
    public void onFirstFrame() {
      super.onFirstFrame();
      setAlpha(1.0f);
    }

    @Override
    public void onPostResume() {
      // Overriding default behavior to avoid dictating system UI via PlatformPlugin.
      lifecycleMessages.send("AppLifecycleState.resumed");
    }
  };
  if (initialRoute != null) {
    flutterView.setInitialRoute(initialRoute);
  }
  lifecycle.addObserver(new LifecycleObserver() {
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
      final FlutterRunArguments arguments = new FlutterRunArguments();
      arguments.bundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
      arguments.entrypoint = "main";
      flutterView.runFromBundle(arguments);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
      flutterView.onStart();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
      flutterView.onPostResume();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
      flutterView.onPause();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
      flutterView.onStop();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
      flutterView.destroy();
    }
  });
  flutterView.setAlpha(0.0f);
  return flutterView;
}
 
Example 4
Source File: Flutter.java    From CrazyDaily with Apache License 2.0 2 votes vote down vote up
/**
 * Initiates the Dart VM. Calling this method at an early point may help decreasing time to first
 * frame for a subsequently created {@link FlutterView}.
 *
 * @param applicationContext the application's {@link Context}
 */
public static void startInitialization(@NonNull Context applicationContext) {
  FlutterMain.startInitialization(applicationContext);
}
 
Example 5
Source File: Flutter.java    From o2oa with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Initiates the Dart VM. Calling this method at an early point may help decreasing time to first
 * frame for a subsequently created {@link FlutterView}.
 *
 * @param applicationContext the application's {@link Context}
 */
public static void startInitialization(@NonNull Context applicationContext) {
  FlutterMain.startInitialization(applicationContext);
}