• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Sharing complex data between activities

What is the best way to share complex data between activities? Suppose Activity A reads data from a database into a data structure, and presents some graphs or whatever based on that data structure. Then the user launches Activity B, which modifies the data in the database. When they return to Activity A, I want the UI to update correctly to reflect any changes made.

I'm new to Android, but not to programming, but the independence of the activities has thrown me a bit. I think what I'd really like is for the data structure created in A to be available in B, and for A to simply refresh the UI in onRestart() or similar. What is the correct Android idiom for achieving this? A singleton seems appealing, but I understand there might be issues related to singletons if the process is killed when the system needs resources.
 
I recently found a super article which presents a nice architecture for interacting with the database, and separating this out from the view aspects of your app. It also introduces something called 'LiveData', which you can use to automatically update the UI when data changes in the database. It's all based on the Observable design pattern.
If you're not already using the Room ORM framework, then I recommend you consider it.

https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0

Once you get to understand how it fits together, believe me, it will vastly improve your code maintanability.
 
Back
Top Bottom