Fix messages from Unity on Android when using multiple Flutter engines. #46
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bug Description
Fixes #35 and #13
Adding certain plugins to your project will result in Unity messages not being received on Android.
This is because these plugins launch additional Flutter Engines for background functionality, which triggers
onAttachedToEngine
multiple times.This causes Unity to send messages to a method channel attached to the wrong Flutter engine.
Some of the many plugins that appear to be capable of doing this:
This happens for both
FlutterActivity
andFlutterFragmentActivity
.Flutter has an open issue that this is expected behaviour and needs documentation in the plugin docs.
Reproduction steps
Tapping on the Flutter logo increments the number in the text "has been touched x times".
The
onAttachedToEngine
log occurs once in the debug console.android_alarm_manager_plus: ^4.0.7
The debug console contains 3 logs of
onAttachedToEngine
.You can only test this with Unity 6000 if you apply #45 first , otherwise touch input is simply broken.
Fix
Only setup the method channel for Unity when there is an Activity attached to the Flutter engine.
The additional Flutter engines used in these plugins do not have an Activity.
The method channel is now created in
onAttachedToActivity
.The changes to the file in the 2022_3 and 6000_0 android plugins are identical.
Alternative
I posted an alternative quick fix here. Which creates a method channel for all Flutter engines and sends the message from Unity to all engines.
The fix in this PR leads to fewer redundant messages being passed.