@@ -76,6 +76,14 @@ public IBinder onBind(Intent intent) {
76
76
public void onDestroy () {
77
77
Log .d ("OverLay" , "Destroying the overlay window service" );
78
78
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
+ }
79
87
NotificationManager notificationManager = (NotificationManager ) getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
80
88
notificationManager .cancel (OverlayConstants .NOTIFICATION_ID );
81
89
if (CachedMessageChannels .overlayMessageChannel != null ){
@@ -90,20 +98,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
90
98
mResources = getApplicationContext ().getResources ();
91
99
boolean isCloseWindow = intent .getBooleanExtra (INTENT_EXTRA_IS_CLOSE_WINDOW , false );
92
100
if (isCloseWindow ) {
93
- if (windowManager != null ) {
94
- windowManager .removeView (flutterView );
95
- windowManager = null ;
96
- flutterView .detachFromFlutterEngine ();
97
- stopSelf ();
98
- }
99
- isRunning = false ;
101
+ stopSelf ();
100
102
return START_STICKY ;
101
103
}
102
- if (windowManager != null ) {
104
+ if (windowManager != null && flutterView != null ) {
103
105
windowManager .removeView (flutterView );
104
106
windowManager = null ;
107
+ }
108
+ if (flutterView != null ) {
105
109
flutterView .detachFromFlutterEngine ();
106
- stopSelf () ;
110
+ flutterView = null ;
107
111
}
108
112
isRunning = true ;
109
113
Log .d ("onStartCommand" , "Service started" );
@@ -128,7 +132,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
128
132
129
133
BasicMessageChannel <Object > overlayMessageChannel = new BasicMessageChannel <>(FlutterEngineCache .getInstance ().get (OverlayConstants .CACHED_TAG ).getDartExecutor ().getBinaryMessenger (), OverlayConstants .MESSENGER_TAG , JSONMessageCodec .INSTANCE );
130
134
overlayMessageChannel .setMessageHandler ((message , reply ) -> {
131
- if (CachedMessageChannels .mainAppMessageChannel == null ) {
135
+ if (CachedMessageChannels .mainAppMessageChannel == null ) {
132
136
reply .reply (false );
133
137
return ;
134
138
}
0 commit comments