[Android] Avoid plugin auto-registration during FlutterFragmentActivity recreate timing window#182963
Conversation
…ty recreate timing window
There was a problem hiding this comment.
Code Review
This pull request addresses a timing issue in FlutterFragmentActivity during activity recreation, which could lead to incorrect plugin auto-registration. The fix involves modifying configureFlutterEngine to explicitly check for an existing FlutterFragment from the FragmentManager if the activity's flutterFragment field is not yet populated. This prevents GeneratedPluginRegister.registerGeneratedPlugins from being called unnecessarily. The change is accompanied by two new unit tests: one to specifically validate the new logic in configureFlutterEngine, and another integration test using ActivityScenario to verify the fix during activity recreation with a cached engine.
This PR fixes an Android embedding timing issue in
FlutterFragmentActivityduring Activity recreation.When an Activity is being recreated,
FragmentManagercan restore/attach an existingFlutterFragmentbeforeensureFlutterFragmentCreated()refreshes theflutterFragmentfield.In that window,
configureFlutterEngine()may run withflutterFragment == null, miss the injected-engine guard, and incorrectly callGeneratedPluginRegister.registerGeneratedPlugins(...).That can lead to unintended plugin lifecycle behavior in real apps.
Pre-launch Checklist
//github.com/).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.