简体   繁体   中英

Android: Broadcast data to multiple Activities within an application

What is the correct pattern to broadcast data changes within an Android application? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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