Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice MVVM pass Data from one Activity to another

What is the current best practice of passing Data from one activity (master) to another (detail).

  1. a possible approach could be to have a single view model class that's shared between the master and detail. When clicking on an item in the master activity the selected entry will be set to the view model. The detail activity, thereby can read the selected entry, because it's using the same view model.

  2. Pass the row-id of the selected object from the master-activity as an bundle-extra to the detail activity. The detail activity loads it's view model by using ViewModelProviders and afterwards passes the row-id to the view-model which loads the actual record.

  3. Initiale the view model before starting the detail activity and set the selected object directly to the initialized view model of the detail activity.

Input would be very much appreciated!

like image 460
Sascha Held Avatar asked Jan 30 '18 22:01

Sascha Held


People also ask

How do I transfer data from one activity to another?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

Can we share ViewModel between activities?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.

How do I transfer data from one ViewModel to another ViewModel?

To pass data from the Main View/Main ViewModel to the Detail ViewModel, assign the data to the ViewModelExtensions. Parameter attached property on the Detail View instance.

How do you send data from ViewModel to activity?

Passing Data between fragments in Android using ViewModel: This is because ViewModel is tied to the activity lifecycle. To actually pass the data between fragments, we need to create a ViewModel object with an activity scope of both the fragments, initialize the ViewModel , and set the value of the LiveData object.


1 Answers

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.

like image 130
Shalbert Avatar answered Oct 03 '22 01:10

Shalbert