I did a lot of research but couldn't find a definitive answer about this question. What is the best way to communicate custom components (Like and Async task) with and Activity?
Currently I'm using this approach:
- Declaring an interface for the component
- Register the interface on the Activity
- Maintain a fragment with
setRetainInstance(true)
on the Activity, registering the component's interface every time the Activity is recreated - Passing the result to the Activity using the interface
My doubt is about the item 3. As far as I understand there will be a moment when the Activity is completely destroyed, and the Custom Components is registered with a null callback ( My fragment unregister it when onDestroy is called ). So, if the component completes it's operation at this time, there won't be any recipients for the message available at the moment. The result could be lost.
I count't reproduce this scenario. But I believe it exists ( by all means, correct me if I'm wrong ).
I considered using a loop with some delay and a counter ( to limit the attempts ) that executes util a callback is found. But this 'solution' looks like a clumsy joke. I also considered using a EventBus or som other library, but I don't know if it work correctly.
Thanks =)
Aucun commentaire:
Enregistrer un commentaire