How to implement the concept of events and delegates in Android?

I have an activity that calls a web service and does xml parsing. I want my activity to wait for the xml parsing class to complete, and then I want my activity to continue. I was wondering if there is an event delegate concept present in android with which I can make my xml parsing class respond to my actions when it ends.

+4
android
Mar 01 2018-11-11T00:
source share
1 answer

Yes there is. You will love the ResultReceiver class. To create it, you need to pass the Handler (created inside the action) and override the onReceiveResult method.

So what you do is send the ResultReceiver link to the service (using the optional Intent options), and when the XML parsing is done, you call the send method from Service . This way, your activity will be notified when XML parsing is complete.

There is a Google IO video explaining this technique. You can also download the slides used in the conference.

If you want to use the sample code, look at the iosched application. It tells how to create a ResultReceiver proxy server that will help you deal with configuration changes (for example, changes in the device) ... because, as you know, when this happens, the user interface is recreated, which can lead to memory leaks (you know, the service will point to non-existent user interface elements).

+9
Mar 01 2018-11-11T00:
source share



All Articles