Skip to content

Defer MixpanelFlutterPlugin registration and initialization to prevent ANRs #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jaredmixpanel
Copy link
Collaborator

@jaredmixpanel jaredmixpanel commented May 22, 2025

Defer MethodChannel setup from onAttachedToEngine into handleInitialize(...), so the channel is only created when Dart calls initialize(). This lazy registration prevents blocking the main thread during engine startup (including headless/Firebase isolates) and eliminates the ANR.

Key Changes
• Move new MethodChannel(...) + setMethodCallHandler(this) into handleInitialize() guarded by channel == null && flutterPluginBinding != null
• Leave onAttachedToEngine() free of any codec or messenger work
• Ensure flutterPluginBinding is stored on attach for later use in initialization

@jaredmixpanel jaredmixpanel requested a review from Copilot May 22, 2025 23:22
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR defers the registration and initialization of the MixpanelFlutterPlugin to prevent potential ANRs during plugin registration. It implements lazy initialization for the MethodChannel, stores the FlutterPluginBinding for later use, and ensures proper cleanup in onDetachedFromEngine.

  • Lazy initialization of the MethodChannel in handleInitialize
  • Storing the FlutterPluginBinding for deferred channel registration
  • Cleaning up channel and binding references in onDetachedFromEngine

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
android/src/main/java/com/mixpanel/mixpanel_flutter/MixpanelFlutterPlugin.java Defers channel registration and adds lazy initialization to avoid ANRs
README.md Updates Markdown formatting for consistency in the “I want to know more!” section

@jaredmixpanel jaredmixpanel marked this pull request as ready for review May 22, 2025 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant