diff --git a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java index 6c5ce2c..2d1cfe2 100644 --- a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java +++ b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java @@ -6,6 +6,7 @@ import android.net.Uri; import android.os.Build; import android.provider.Settings; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -51,7 +52,7 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin } @Override - public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { + public synchronized void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { pendingResult = result; if (call.method.equals("checkPermission")) { result.success(checkOverlayPermission()); @@ -110,7 +111,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { } else { result.notImplemented(); } - } @Override diff --git a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java index f8aad1a..db3920c 100644 --- a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java +++ b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java @@ -76,6 +76,12 @@ public IBinder onBind(Intent intent) { public void onDestroy() { Log.d("OverLay", "Destroying the overlay window service"); isRunning = false; + if (windowManager != null) { + windowManager.removeView(flutterView); + windowManager = null; + } + flutterView.detachFromFlutterEngine(); + flutterView = null; NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(OverlayConstants.NOTIFICATION_ID); if(CachedMessageChannels.overlayMessageChannel != null){ @@ -89,21 +95,9 @@ public void onDestroy() { public int onStartCommand(Intent intent, int flags, int startId) { mResources = getApplicationContext().getResources(); boolean isCloseWindow = intent.getBooleanExtra(INTENT_EXTRA_IS_CLOSE_WINDOW, false); - if (isCloseWindow) { - if (windowManager != null) { - windowManager.removeView(flutterView); - windowManager = null; - flutterView.detachFromFlutterEngine(); - stopSelf(); - } - isRunning = false; - return START_STICKY; - } - if (windowManager != null) { - windowManager.removeView(flutterView); - windowManager = null; - flutterView.detachFromFlutterEngine(); + if (isCloseWindow || windowManager != null) { stopSelf(); + return START_STICKY; } isRunning = true; Log.d("onStartCommand", "Service started"); @@ -128,7 +122,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { BasicMessageChannel overlayMessageChannel = new BasicMessageChannel<>(FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG).getDartExecutor().getBinaryMessenger(), OverlayConstants.MESSENGER_TAG, JSONMessageCodec.INSTANCE); overlayMessageChannel.setMessageHandler((message, reply) -> { - if(CachedMessageChannels.mainAppMessageChannel == null) { + if (CachedMessageChannels.mainAppMessageChannel == null) { reply.reply(false); return; }