How to implement event and delegate concept in Android?

后端 未结 1 686
别跟我提以往
别跟我提以往 2020-12-30 17:43

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 w

相关标签:
1条回答
  • 2020-12-30 18:08

    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 讨论(0)
提交回复
热议问题