Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement event and delegate concept in Android?

Tags:

android

I have a activity which calls a webservice and does xml parsing. i want my activity to wait for the xml parsing class to execute and then i want my acitvity to continue. i was wondering if there is an event delegate concept that is present in android by which i can make my xml parsing class respond to my activity when it is over.

like image 283
user590849 Avatar asked Jan 21 '23 08:01

user590849


1 Answers

Yes, there's. You will love the ResultReceiver class. To create one you will need to pass a Handler (created inside the activity), and override the onReceiveResult method.

So, what you do is sending a reference of the ResultReceiver to the service (using the Intent extras), and when the XML parsing is done, you call the send method from the Service. That way your activity will be notified that the XML parsing has finished.

There's a Google IO video where this technique is explained. You can also download the slides used in the conference.

If you want example code, take a look at the iosched app. It will teach how to create a ResultReceiver proxy that will help you deal with configuration changes (e.g. the device rotation changes)... because, as you know, when that happens the UI is recreated, thus it could cause memory leaks (you know, the service will be pointing to non-existing UI elements).

like image 59
Cristian Avatar answered Feb 01 '23 12:02

Cristian