How To Start An Activity From Flutter Plugin Using An Api
So I am making a Flutter plugin and I am attempting to run Kotlin code on Android. The problem is, this code runs a method which attempts to start an activity without the FLAG_ACTI
Solution 1:
You can extend your plugin to implement ActivityAware
in your plugin class, when you implement it, you get a couple of callbacks that gives you the current activity. Like this :
lateinit activity: Activity? = nulloverridefunonDetachedFromActivity() {
activity = null
}
overridefunonReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}
overridefunonAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
overridefunonDetachedFromActivityForConfigChanges() {
activity = null
}
After that you can just startActivity from the assigned activity variable.
Let me know if you need further help.
Solution 2:
As you mentioned, For Flutter plugin any platform-dependent logics should be kept in the subclass of FlutterActivity which was used to show flutter module/screens inside a native module. Now you can launch intent from that subclass without any additional flags.
@note - Subclass of FlutterActvity should be kept in the native module.
classFlutterResponseActivity : FlutterActivity() {
privatevar methodResult: Result? = nulloverridefunprovideFlutterEngine(context: Context): FlutterEngine? {
return MyApplication.mContext.flutterEngine //Pre-warmed flutter engine
}
overridefunconfigureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"startMainActivity" -> {
startMainActivity()
result.success(true)
}
else -> result.notImplemented()
}
}
}
privatefunstartMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
Post a Comment for "How To Start An Activity From Flutter Plugin Using An Api"