1919
2020import io .flutter .embedding .engine .plugins .FlutterPlugin ;
2121import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
22+ import io .flutter .plugin .common .BinaryMessenger ;
2223import io .flutter .plugin .common .MethodCall ;
2324import io .flutter .plugin .common .MethodChannel ;
2425import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
4748public class OptimizelyFlutterSdkPlugin extends OptimizelyFlutterClient implements FlutterPlugin , ActivityAware , MethodCallHandler {
4849
4950 public static MethodChannel channel ;
51+ private static BinaryMessenger attachedMessenger ;
5052 private Appender <ILoggingEvent > flutterLogbackAppender ;
5153
5254 /**
@@ -215,7 +217,8 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
215217 if (channel != null ) {
216218 return ;
217219 }
218- channel = new MethodChannel (binding .getBinaryMessenger (), "optimizely_flutter_sdk" );
220+ attachedMessenger = binding .getBinaryMessenger ();
221+ channel = new MethodChannel (attachedMessenger , "optimizely_flutter_sdk" );
219222 channel .setMethodCallHandler (this );
220223 context = binding .getApplicationContext ();
221224
@@ -234,8 +237,12 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
234237
235238 @ Override
236239 public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
240+ if (binding .getBinaryMessenger () != attachedMessenger ) {
241+ return ;
242+ }
237243 channel .setMethodCallHandler (null );
238244 channel = null ;
245+ attachedMessenger = null ;
239246 // Stop and detach the appender
240247 if (flutterLogbackAppender != null ) {
241248 Logger rootLogger = (Logger ) LoggerFactory .getLogger (Logger .ROOT_LOGGER_NAME );
0 commit comments