how can I listen, in an activity, to an event triggered in another activity?
More specifically, I created a menu with an activity to connect to facebook and get the user’s friends (as soon as the user is logged, the friend list is automatically and asynchronously requested and, on onComplete event, fills a global ArrayList). Say the user leaves the menu before the request is completed, and goes to another activity which shows the list of friends. To fill the list, I need to know if the getfriends request is complete and the ArrayList is filled. If it’s not, I need to know when it is done to start filling my list of friends. I’m not sure whether the asynchronous request is killed when leaving the activity, and, if not, how can I know it’s completed.
Can it be achieved?
Thank you
OBSOLETE:
The basis for inter-activity communication in Android is Intents, which, basically, is event-based (a)synchronous communication. I take it that the events you refer to may be anything that happens.
You should probably just define an application specific Intent and fire it off at the activity with the listview when the Facebook API has completed its request.
EDITED:
…and, OK, you’re after something slightly different. Activities can and will be killed willy-nilly: this should leave asynch requests hanging (but I haven’t looked at the FB API’s, so I’m just guessing). I think what you are describing requires the Facebook request to be made from a Service within your application. Then you should be able just to use the basic Observable/Observers: declare a
FriendObservable(simplest implementation: a global object or singleton) to which any Activity displaying friends can register itself. I think you should limit the lifetime of such aFriendServiceto Facebook API interactions, to minimize resource consumption.