0

I have an activity with multiple fragment views (they get replaced with other fragments)

One of these fragments has an ASyncTask. This ASyncTask queries a webserver, parses the response. Inserts the response data into a database and finally updates the UI using the onPostExecute method.

Now I am planning to update the database,by querying the webserver, every hour. But now I am struggling with how I am going to implement this.

I would like to combine the asynctask and the hourly update into one task. I guess it would be best to use a IntentService with an AlarmManager but I also need to update the UI if the request came from a Fragment.

Is their any elegant/simple way to implement this feature.

Sidenotes: i need the fragment ui also to update after an orientation change or when the user pauses the app.

Brianvdb
  • 666
  • 1
  • 6
  • 16

1 Answers1

0

Basically there are 2 options for updating UI after database update: fast+simple and better, but 'longer'.

Community
  • 1
  • 1
thevery
  • 172
  • 9
  • Thanks, this answers a part of my question. I think the BroadcastReceiver will do the job for me but how should I update the database. Should I use a custom IntentService to update the data and than send a broadcast from there? – Brianvdb Feb 27 '15 at 06:46
  • Yes. `AsyncTask`s are usually used for UI-bound tasks only while `Service`s (and especially `IntentService`s) are used for background and non-UI-bound tasks (and usually non-repeating - there is a little overhead for starting and stopping). – thevery Feb 27 '15 at 11:13
  • I ended up using the commonsware WakefulIntentService along with a BroadcastReceiver to update my fragment. Thanks for the help. – Brianvdb Feb 27 '15 at 17:00