开发者

How to implement event and delegate concept in Android?

开发者 https://www.devze.com 2023-02-14 06:56 出处:网络
I have a activity which calls a webservice and does xml parsing. i want my activity to wai开发者_开发技巧t for the xml parsing class to execute and then i want my acitvity to continue. i was wondering

I have a activity which calls a webservice and does xml parsing. i want my activity to wai开发者_开发技巧t 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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消