Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: The app is still running in background event if it's killed #1395

Open
3 of 8 tasks
robpot95 opened this issue Dec 18, 2023 · 17 comments
Open
3 of 8 tasks

[Bug]: The app is still running in background event if it's killed #1395

robpot95 opened this issue Dec 18, 2023 · 17 comments
Labels
background-foreground-location P2 Important issues not at the top of the work list. platform: android Issue is related to the Android platform. type: bug Something isn't working

Comments

@robpot95
Copy link

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Steps to reproduce

Just have notification configured and run the app. When you kill it you get:
There is still another flutter engine connected, not stopping location service

Expected results

The app should be killed and notification should be removed

Actual results

The app still is running and notification is still there. This is draining users battery.

Code sample

Code sample
[Paste your code here]

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

10.1.0

Flutter Doctor output

Doctor output
[Paste your output here]
@TimHoogstrate
Copy link
Contributor

Dear @robpot95,

Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?

Kind regards,

@TimHoogstrate TimHoogstrate added the status: needs more info We need more information before we can continue work on this issue. label Jan 4, 2024
@robpot95
Copy link
Author

robpot95 commented Jan 4, 2024

Dear @robpot95,

Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?

Kind regards,

Hello,

I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app.

Also this happens only if i configure foregroundNotificationConfig

@github-actions github-actions bot removed the status: needs more info We need more information before we can continue work on this issue. label Jan 4, 2024
@XERZES27
Copy link

XERZES27 commented Jan 6, 2024

Same thing here, the foregroundNotificationConfig ensures that the notification is non dismissible even if the application has been terminated, it has not been the case for me that the app remains alive. But the notification seems permanent.

Dear @robpot95,
Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?
Kind regards,

Hello,

I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app.

Also this happens only if i configure foregroundNotificationConfig

@robpot95
Copy link
Author

robpot95 commented Jan 8, 2024

Same thing here, the foregroundNotificationConfig ensures that the notification is non dismissible even if the application has been terminated, it has not been the case for me that the app remains alive. But the notification seems permanent.

Dear @robpot95,
Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?
Kind regards,

Hello,
I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app.
Also this happens only if i configure foregroundNotificationConfig

Well the problem is that the app does not get killed and somehow the geolocator attach itself to something else to keep it self alive.

@mvanbeusekom mvanbeusekom added type: bug Something isn't working platform: android Issue is related to the Android platform. P2 Important issues not at the top of the work list. background-foreground-location labels Jan 15, 2024
@mvanbeusekom
Copy link
Member

Thank you for reporting this issue, I have labelled it as a bug and added it to our backlog.

@Wackymax
Copy link
Contributor

This happens because we do flutter engine reference counting to keep the location service alive when location is used in conjunction with another package like flutter_background so that location service can be used in Android Services and not be dependent on an Activity that can be killed by Android. My suggestion would be that you listen to the activity lifecycle in Flutter and cancel your location stream when the activity is killed.

@alaatm
Copy link

alaatm commented Feb 4, 2024

Flutter's lifecycle management detached is never called on android when the app is killed by the user, same as dispose() as mentioned here (flutter/flutter#40940).

This causes the message "running in background" still showing when the app is terminated by the user and it stays forever unless user force stops the app. Can we use MainActivity.onDestroy on the native side to stop geolocator and if so, how?

@mvanbeusekom
Copy link
Member

Hi all,

I was looking into this and I noticed that the geolocator keeps hold of the FlutterPluginBinding instance after the onDetachedFromActivity method is being called. In the permission_handler plugin this resulted into leaking the activity which I think might also have an impact on this issue.

Unfortunately this is a bit difficult for me to test, is someone willing to verify if this issue still occurs when using version 4.5.2 of the geolocator_android package?

@Wackymax
Copy link
Contributor

Wackymax commented Feb 7, 2024 via email

@hiromu-kon
Copy link

I've tried updating the geolocator_android package to version 4.5.2 in my environment, but unfortunately, I haven't noticed any changes regarding the issue.

@hiromu-kon
Copy link

@mvanbeusekom
I'm still facing challenges with the issue previously mentioned and I'm really in need of assistance. Despite updating the geolocator_android package to version 4.5.2 as suggested, I've observed no improvements concerning the problem. Our Android app users are currently left uncertain whether the location is being properly accessed, which is quite a critical concern for us.

Any insights or alternative solutions to circumvent this issue would be immensely appreciated. We're eager to resolve this as smoothly and swiftly as possible for the benefit of our users.

@Wackymax
Copy link
Contributor

I think there is a misunderstanding here. If you want the location services to stop when the application is killed then do not configure a foreground notification. The intention of that is to increase the priority of the flutter activity to try and not have it killed. If you do need the location in the background then use a background service plugin that will start a separate background service. Otherwise, remove the foreground notification config so that the location service can shut down when the activity is killed.

@alaatm
Copy link

alaatm commented Mar 22, 2024

@Wackymax

In our app we need to track location updates when the app is running (whether foreground or minimzed). For that, we are utilizing the foreground service as I understand from the android docs that this is the way to do it (https://codelabs.developers.google.com/codelabs/while-in-use-location/#2) under Services, foreground services, and binding section. We do not required location tracking when the app is killed so from my understanding we should not declarate the background location permission as this requires extra approval from Google.

When the app is in foreground and the user swipes it away (terminates it) the location service is stopped. However, when it is minimzed and the user swipes it away the location service is still on and the location notification still persists for the user.

Edit: When location service is started, swiping the app away (regardless whether it was foreground or minimized) keeps the location notification on. The log from geolocator says: There is still another flutter engine connected, not stopping location service.

As I mentioned previously, we cannot rely on lifecycle events to stop the service on terminate events because these events are not raised when the app is terminated.

@Wackymax
Copy link
Contributor

Wackymax commented Mar 22, 2024 via email

@alaatm
Copy link

alaatm commented Mar 24, 2024

I just read about stopWithTask flag in the android docs with the following comment:

If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities

Maybe if this is defined in the manifest file as follows:

<service
                android:enabled="true"
                android:exported="false"
                android:foregroundServiceType="location"
+               android:stopWithTask="true"
                android:name=".GeolocatorLocationService"/>

It will stop the service when the app is killed?

This SO answer suggests the same.

@Tarun-kumar-koda
Copy link

I just read about stopWithTask flag in the android docs with the following comment:

If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities

Maybe if this is defined in the manifest file as follows:

<service
                android:enabled="true"
                android:exported="false"
                android:foregroundServiceType="location"
+               android:stopWithTask="true"
                android:name=".GeolocatorLocationService"/>

It will stop the service when the app is killed?

This SO answer suggests the same.

so, is this working?

@Wackymax
Copy link
Contributor

Wackymax commented May 8, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
background-foreground-location P2 Important issues not at the top of the work list. platform: android Issue is related to the Android platform. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants