Best practice MVVM pass Data from one Activity to another

I had the same question, and it was answered perfectly by Lyla Fujiwara's article ViewModels: Persistence, onSaveInstanceState(), Restoring UI State and Loaders. The article discusses the different ways you can persist data and which is best fit for a given occasion.

Do ViewModels persist my data? TL;DR No. Persist as normal!

Are ViewModels a replacement for onSaveInstanceState? TL;DR No, but they are related so keep reading.

How do I use ViewModels to save and restore UI state efficiently? TL;DR You use a combination of ViewModels, onSaveInstanceState() and local persistence.

Are ViewModels a replacement for Loaders? TL;DR. Yes, ViewModels used in conjunction with a few other classes can replace Loaders.

Those are here succinct answers to questions tangential to what you're asking. If you read the actual article, she gives explanations for each.


Your detail activity should be able to reconstruct itself from saved state. For example, with your detail in the foreground and the screen off, your entire app could be expunged from memory. When the screen is back on, Android will only start your detail activity and expect it to get what it needs from saved state.

So, any design that relies on the master setting data into a singleton / global somewhere would not be great. Unclear to me exactly but it seems like that's what you might have been suggesting in (1) and (3).

IMHO, set the row ID into the extras passed to the detail activity. Save / restore that row ID with detail save state. Let the detail activity build it's own model based on the row ID. It makes the detail independent in that it won't depend on something else initializing some complex model before it can start. That makes it more modular and testable also.