@@ -76,6 +76,14 @@ public IBinder onBind(Intent intent) {
7676 public void onDestroy () {
7777 Log .d ("OverLay" , "Destroying the overlay window service" );
7878 isRunning = false ;
79+ if (windowManager != null && flutterView != null ) {
80+ windowManager .removeView (flutterView );
81+ windowManager = null ;
82+ }
83+ if (flutterView != null ) {
84+ flutterView .detachFromFlutterEngine ();
85+ flutterView = null ;
86+ }
7987 NotificationManager notificationManager = (NotificationManager ) getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
8088 notificationManager .cancel (OverlayConstants .NOTIFICATION_ID );
8189 if (CachedMessageChannels .overlayMessageChannel != null ){
@@ -90,20 +98,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9098 mResources = getApplicationContext ().getResources ();
9199 boolean isCloseWindow = intent .getBooleanExtra (INTENT_EXTRA_IS_CLOSE_WINDOW , false );
92100 if (isCloseWindow ) {
93- if (windowManager != null ) {
94- windowManager .removeView (flutterView );
95- windowManager = null ;
96- flutterView .detachFromFlutterEngine ();
97- stopSelf ();
98- }
99- isRunning = false ;
101+ stopSelf ();
100102 return START_STICKY ;
101103 }
102- if (windowManager != null ) {
104+ if (windowManager != null && flutterView != null ) {
103105 windowManager .removeView (flutterView );
104106 windowManager = null ;
107+ }
108+ if (flutterView != null ) {
105109 flutterView .detachFromFlutterEngine ();
106- stopSelf () ;
110+ flutterView = null ;
107111 }
108112 isRunning = true ;
109113 Log .d ("onStartCommand" , "Service started" );
@@ -128,7 +132,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
128132
129133 BasicMessageChannel <Object > overlayMessageChannel = new BasicMessageChannel <>(FlutterEngineCache .getInstance ().get (OverlayConstants .CACHED_TAG ).getDartExecutor ().getBinaryMessenger (), OverlayConstants .MESSENGER_TAG , JSONMessageCodec .INSTANCE );
130134 overlayMessageChannel .setMessageHandler ((message , reply ) -> {
131- if (CachedMessageChannels .mainAppMessageChannel == null ) {
135+ if (CachedMessageChannels .mainAppMessageChannel == null ) {
132136 reply .reply (false );
133137 return ;
134138 }
0 commit comments