@@ -78,13 +78,15 @@ public IBinder onBind(Intent intent) {
7878 @ Override
7979 public void onDestroy () {
8080 Log .d ("OverLay" , "Destroying the overlay window service" );
81- if (windowManager != null ) {
81+ isRunning = false ;
82+ if (windowManager != null && flutterView != null ) {
8283 windowManager .removeView (flutterView );
8384 windowManager = null ;
85+ }
86+ if (flutterView != null ) {
8487 flutterView .detachFromFlutterEngine ();
8588 flutterView = null ;
8689 }
87- isRunning = false ;
8890 NotificationManager notificationManager = (NotificationManager ) getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
8991 notificationManager .cancel (OverlayConstants .NOTIFICATION_ID );
9092
@@ -104,20 +106,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
104106 int startY = intent .getIntExtra ("startY" , OverlayConstants .DEFAULT_XY );
105107 boolean isCloseWindow = intent .getBooleanExtra (INTENT_EXTRA_IS_CLOSE_WINDOW , false );
106108 if (isCloseWindow ) {
107- if (windowManager != null ) {
108- windowManager .removeView (flutterView );
109- windowManager = null ;
110- flutterView .detachFromFlutterEngine ();
111- stopSelf ();
112- }
113- isRunning = false ;
109+ stopSelf ();
114110 return START_STICKY ;
115111 }
116- if (windowManager != null ) {
112+ if (windowManager != null && flutterView != null ) {
117113 windowManager .removeView (flutterView );
118114 windowManager = null ;
115+ }
116+ if (flutterView != null ) {
119117 flutterView .detachFromFlutterEngine ();
120- stopSelf () ;
118+ flutterView = null ;
121119 }
122120 isRunning = true ;
123121 Log .d ("onStartCommand" , "Service started" );
@@ -147,7 +145,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
147145
148146 BasicMessageChannel <Object > overlayMessageChannel = new BasicMessageChannel <>(FlutterEngineCache .getInstance ().get (OverlayConstants .CACHED_TAG ).getDartExecutor ().getBinaryMessenger (), OverlayConstants .MESSENGER_TAG , JSONMessageCodec .INSTANCE );
149147 overlayMessageChannel .setMessageHandler ((message , reply ) -> {
150- if (CachedMessageChannels .mainAppMessageChannel == null ) {
148+ if (CachedMessageChannels .mainAppMessageChannel == null ) {
151149 reply .reply (false );
152150 return ;
153151 }
0 commit comments