How to get or cache the main flutter engine?

Issue

My goal is to display a screen even if the phone is locked I looked for a solution to do that in flutter but I haven’t found one. So I want to start a new FlutterActivity in android native code but I don’t want to create a new flutter engine. Is it possible to access the main flutter engine which is created when the app is launched or to manually create the main engine when the app is launched and cache it ?

Solution

I finally found a solution. I simply override the configureFlutterEngine(FlutterEngine flutterEngine) method of the FlutterActivity class. There is no need to cache it because it is easy to get it in the MainActivity class, but in case someone needs it:

FlutterEngineCache
  .getInstance ()
  .put("engine_id", flutterEngine);

Answered By – sitatech

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.