如何通知不活动的活动?

发布于 2024-10-21 06:50:52 字数 382 浏览 7 评论 0原文

我的计划有几项活动。当一个活动位于前台时,用户可以在那里执行某些操作,并且应通知其他不活动(即停止和暂停)的活动,以便当它们 onResume() 时,它们可以对其进行相应的操作。

我的问题是:前台活动如何枚举是否存在以及存在哪些非活动活动?它如何通知他们?

我查看了意图和广播,它们似乎不是正确的答案。我尝试将事件发布到非活动活动的处理程序队列,但由于我不知道如何枚举它们,所以我也无法访问它们的处理程序。

帮助!谢谢你!

(我不能只设置一个全局标志。因为当第一个非活动活动变为活动时,它会看到它,并清除它。然后 IA 的其余部分将不知道。我不能为每个人设置一个计数器标志来减一吧,因为我不知道那里有没有IA,有多少IA,在我看来,唯一的办法就是我知道那里有谁,然后一一通知他们。)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最笨的告白 2024-10-28 06:50:52

我的问题是:前台活动如何枚举是否存在以及存在哪些非活动活动?它如何通知他们?

你不这样做,你也不这样做。

想象一个具有多个网页的 Web 应用程序。 “前台网页”不会“枚举是否存在以及存在哪些非活动网页”,更不用说“通知它们”了。从当前可见的网页的角度来看,它是唯一的东西。浏览器在其历史记录中可能碰巧有其他网页这一事实并不重要。

Android 活动也是如此。一个活动不应该知道也不关心 RAM 中是否有来自该应用程序的 0、1、2 或 1337 个其他活动(尽管有可能,它会远低于 1337)。相反,应该有一个一致的中央数据模型(例如数据库),并且每个活动都针对中央数据模型进行工作。

好的,这是否意味着 Android 不支持或鼓励一项活动主动通知另一项不活动的活动?

正确的。

所以活动必须轮询才能获取信息?

就像网页必须轮询才能获取信息一样。这在某种程度上取决于您对“民意调查”的定义。

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

别理我 2024-10-28 06:50:52

首先我怀疑你的想法是否正确。一个活动应该请求启动信息,并且不应该被另一个活动“通知”(我不谈论 startActivityForResult())。

当不同活动之间有大量数据共享时,您应该认真考虑将它们存储在配置类、SharedPreferences 或数据库中。你的主要逻辑问题是你所说的:

我不能只设置一个全局标志。因为当第一个非活动活动变为活动时,它会看到它并清除它。

为什么一项活动应该清除标志?仅当您告诉它清除标志时,它才会清除该标志。因此,由您来阻止该活动清除您的标志。

First I doubt that you think in the right way. An activity should ask for information on start up and shouldn't be "informed" by another activity (I don't talk about startActivityForResult()).

When you have a lot of data sharing between different activities, you should seriously consider storing them in a configuration class, in SharedPreferences or in the database. You main logic problem is what you said:

I can't just set a global flag. Because when the first inactive activity becomes activity, it sees it, it clears it.

Why ever should an activity clear a flag? It only clears a flag when you tell it to clear it. So thats up to you to stop the activity from clearing your flag.

策马西风 2024-10-28 06:50:52

您可以考虑使用内容提供程序并调用 ContentResolver.notifyChange() 让感兴趣的活动知道某些内容已更改。

You could consider using a content provider and calling ContentResolver.notifyChange() to let interested activities know something has changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文