Android Flutter launch custom activity with cached engine
Asked Answered
D

2

6

Iam integrating FlutterActivity to a native Android app. I have CustomFlutterActivity which inherits from FlutterActivity, which I want to launch using cached FlutterEngine.

This is the code from the documentation for how to do this:

startActivity(
      FlutterActivity
        .withNewEngine()
        .build(currentActivity)
      );

What I want to achieve is to launch my CustomFlutterActivity with my cached engine (and not a generic FlutterActivity as the documentation says)

Devoir answered 16/6, 2020 at 14:32 Comment(0)
G
4

In your CustomFlutterActivity which I suppose is derived from FlutterActivity you can override getCachedEngineId and provide the my_engine_id you have previously cached according to the docs, namely:

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

Thus:

class CustomFlutterActivity: FlutterActivity() {
    override fun getCachedEngineId(): String? {
        return "my_engine_id"
    }
}

See docs

Gaspard answered 23/10, 2020 at 12:14 Comment(0)
S
0

You can override provideFlutterEngine and return FlutterEngine from the cache.

class CustomFlutterActivity : FlutterActivity() {
    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return FlutterEngineCache.getInstance().get("flutter_engine")
    }
}
Socio answered 13/12, 2021 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.