Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android IntentService providing data back to Activity

I have a activity (ListActivity) in which if a user click on a button, I kick off an intent to a "IntentService" which basically makes a REST call to a web service and gathers data. My problem is that once the service is done, i populate list of items in the Activity which is just a static var. However, I am having trouble kicking off listadaptor to ask it to refresh the view as the data has changed. Can someone tell me how can i "notify" the activity to refresh as the IntentService has completed it task?

like image 225
bond Avatar asked Jan 19 '23 23:01

bond


1 Answers

If you're making a REST call, I would suggest you use IntentService instead of an AsyncTask. AsyncTask looks nice and useful, but it needs much care not to leak the context and return the result to an Activity that might be as well long gone, which makes it not that easy to properly use.

Please watch this presentation for very useful guidelines:

http://www.youtube.com/watch?v=xHXn3Kg2IQE

EDIT: As for sending back the result, you can include a parcelable ResultReceiver in your Intent that you start the IntentService with, and notify the Activity (and handle the adapter refresh there) using .send()

As for refreshing the ListAdapter, it really depends on which kind you are using. If it's a simple adapter, you could swap it using

getListView().setAdapter(adapter)

but you'd be much better with CursorAdapter and simply calling

context.getContentResolver().notifyChange(uri, null);

Where context is the Context of your IntentService.

Hope that helps!

like image 83
karni Avatar answered Jan 25 '23 14:01

karni