Fetch stops downloading when app is in background

Created on 4 Aug 2018  路  10Comments  路  Source: tonyofrancis/Fetch

Is there a way to get on start and stop downloading to keep Fetch alive in a foreground service? Or make Fetch to auto manage its own foreground service?

help wanted

Most helpful comment

as Tony suggested you will need a background job that will be started once WIFI is enabled and there is internet, since you can never know if android will kill your app, or when a user kills your app, new android work manager has a support for that, I think you can tell the work manager if you download is finished or not so it knows not to kill your job, keep the job alive as long as your download is in progress. You can always ask fetch to return to you the status of the job so you can tell android work manager if you job is finished or not.

All 10 comments

@jordyamc Thanks for using Fetch. Different apps require different back grounding strategies. Fetch is easy to implement in a background service or the new worker api that was introduced at Google IO.

Until fetch i was using a foreground Intent Service to do the download work so my app was able to keep downloads alive also when app was in background (avoiding system kill), but now with Fetch i can't figure out how to keep alive Fetch in a Service

I mean, i can keep track of the onStarted and onCompleted/onError to check the availables downloads and Start/Finish the foreground service, but i would like a more direct way to implement it

as Tony suggested you will need a background job that will be started once WIFI is enabled and there is internet, since you can never know if android will kill your app, or when a user kills your app, new android work manager has a support for that, I think you can tell the work manager if you download is finished or not so it knows not to kill your job, keep the job alive as long as your download is in progress. You can always ask fetch to return to you the status of the job so you can tell android work manager if you job is finished or not.

Personally, I explicitly start a service that I keep alive until all downloads have completed.

@Karuhanga how can you make sure that the service is not killed when the user or an android system kills your app process? I think all your app services will be shut down

@abdurahmanadilovic there are instances where you simply can't avoid your service being killed because of resource contraints. If however you are explicitly starting the service via startService, and not starting it by simply binding to it, you can always return START_STICKY(https://developer.android.com/reference/android/app/Service.html#START_STICKY) from onStartCommand, and android will automatically restart your service if it is prematurely killed.
That said, I've also observed Fetch sometimes forgets about the downloads and I've had to (manually) resume them when my service is restarted.

@Karuhanga in that case that is the best approach, much simpler then using the work manager, just have a service running at all times and periodically pool fetch to see if there are outstanding downloads. But how does services behave in android 8.0+, I know intent services cant be started directly anymore, you need to send a request to start a service and it will be started sometimes in the future, is it the same behavior with regular services?

@abdurahmanadilovic Yes, the Android 8+ behavior changes affect all services. I agree, this makes this a little more complicated if your service isn't doing something that's visible in the foreground. I haven't really looked at these restrictions in detail, but if you were looking at an IntentService for example, you'd have to use the JobIntentService instead.

As for how you'd adapt Fetch to these restrictions, that is something worth looking into.

@abdurahmanadilovic To keep a service alive you only need no start it like foreground service (it needs to show a ongoing notification), the service will be alive until you stop it manually or you remove the app from recents apps

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emerging-coders picture emerging-coders  路  3Comments

rs-georg picture rs-georg  路  4Comments

burhanaksendir picture burhanaksendir  路  4Comments

jpvs0101 picture jpvs0101  路  3Comments

MohammadFneish7 picture MohammadFneish7  路  4Comments