简体   繁体   English

Android:将数据广播到应用程序中的多个活动

[英]Android: Broadcast data to multiple Activities within an application

What is the correct pattern to broadcast data changes within an Android application? 在Android应用程序中广播数据更改的正确模式是什么? I have a global data class that will refresh its data based on a user action or a timer. 我有一个全局数据类,它将根据用户操作或计时器刷新其数据。 Each time it refreshes its data, it needs to notify any open Activities that depend on the data. 每次刷新其数据时,它都需要通知任何依赖于该数据的打开的活动。 Basically I have two Activities that need to subscribe to changes. 基本上,我有两个需要订阅更改的活动。

If the data is only being used in one application, you could store it in an object visible to all your activities and use the observer pattern. 如果数据仅在一个应用程序中使用,则可以将其存储在对所有活动可见的对象中,并使用观察者模式。 In your case the global data class will be the observable and the two activities will be observers 在您的情况下,全局数据类将是可观察的,而这两个活动将是观察者

Other types of patterns are of course possible, but you will have a hard time finding something more efficient. 当然也可以使用其他类型的模式,但是您将很难找到更有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM