Search code examples
androidandroid-activitypreferencesflags

How to notify inactive activities?


My program has several activies. When one activity is in the foreground, the user may do something there, and the other inactive (i.e., stopped and paused) activities should be informed so that when they onResume(), they can act upon it accordingly.

My question is: How does the foreground activety enumerate if and what inactive activity exists? And how does it inform them?

I've looked at Intent and Broadcast, they don't seem to be the right answer. I've tried to post an event to the inactive activies' Handler queue, but since I don't know how to enumerate them, so I can't access their handlers either.

Help! Thank you!

(I can't just set a global flag. Because when the first inactive activity becomes activity, it sees it, it clears it. Then the rest of the IA won't know. I can't set a counter flag for everybody to come and decrement one, because I don't know if and how many IA are there. It seems to me that the only way is I get to know who are there and notify them one by one.)


Solution

  • My question is: How does the foreground activety enumerate if and what inactive activity exists? And how does it inform them?

    You don't and you don't.

    Think of a Web app, with multiple Web pages. The "foreground Web page" would not "enumerate if and what inactive Web pages exist", let alone "inform them". From the standpoint of the currently-visible Web page, it is the only thing around. The fact that the browser happens to perhaps have other Web pages in its history is immaterial.

    The same thing goes with Android activities. One activity should neither know nor care whether there are 0, 1, 2, or 1337 other activities from this application in RAM (though, odds are, it will be well below 1337). Rather, there should be a consistent central data model (e.g., database), and the activities each work against the central data model.

    OK, so does it mean that Android does not support or encourage one activity proactively informs another inactive activity?

    Correct.

    So that activies have to poll to get information?

    No more or less than Web pages have to poll to get information. That depends a bit on your definition of "poll".