Issue
I want to run an long running operation in Android. Say the task would run for about 5-10 mins. For this reason I am planning to use a JobIntentService
and Bind it to an Activity
.
Right now I am using a AsyncTask
, even though I know AsyncTask
cannot/should not be used for long running operations hence I am planning to change it now. Many times what happens is while the task is running the user minimizes the app and after sometime the Android OS closes/clears the Activity
to free up some memory.
So my AsyncTask
is kept running without any purpose and crashes while trying to update a view in that Activity
.
So I am planning to use an JobIntentService
. But will using an JobIntentService
and Binding it to an Activity
will reduce the chances of Android OS closing/clearing the Activity
? or still will it follow the same process?
Any Help would be really grateful.
Solution
If your Activity
is in the background then blocking Android from killing your Activity
doesn't sound like a good idea (and even necessary) to me. Since your Activity
is in the background it's not visible to the user and there's no need to update the UI during this time.
Instead, you could do the following:
If Activity
is in the foreground then it can receive updates from your JobIntentService
via BroadcastReceiver
that you register/unregister with LocalBroadcastManager
in onResume/onPause
.
IF JobIntentService
has completed its work when your Activity
was killed or in the background you could persist the result somewhere (for example, in SharedPreferences
) and then in onResume
of your Activity
you could read that result from SharedPreferences
.
Answered By - Anatolii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.