1

In my app I need to load a feed of some data from server(using retrofit) that is loaded in a dedicated viewmodel. What happens currently is that the viewmodel calls the repository in its constrcutor, and then once the data is fetched , the observing fragment in the main activity is notified and changes the ui accordingly.

However, suppose I want to fetch that data during the splash/launcher activity, what would be the implementation for it using MVVM? I mean, since the viewmodel is associated with only one activity, we can't share the same viewmodel accross both activities.

So how could we load the remote server data in Launcher Activity, and then use that in Main Activity?

What are the best practices in this case?

BVtp
  • 2,308
  • 2
  • 29
  • 68

1 Answers1

0

Relation between ViewModel and View is one-to-many. It's a difference from MVP. You can share your ViewModel with other Views and can use separate ViewModels for every View.

UPD: Seems like you can create 1 activity, the content of your Splash and Main activity will be fragments. So, by going from splash to main you will just swap fragments. If you will create a ViewModel in both of fragments as follows:

 ViewModelProviders.of(getActivity()).get(MainViewModel.class);

instead of:

ViewModelProviders.of(this).get(MainViewModel.class);

You will get the same instance of ViewModel in both fragments.

Nazarii Moshenskiy
  • 1,802
  • 2
  • 16
  • 35
  • how do you share a viewmodel between activity A (launcher/splash), and some fragment in activity B (fragment that holds the data ui in the main activity)? Using the ViewModelProviders will result in two different viewmodels, am I not correct? – BVtp Dec 21 '18 at 06:22
  • @BVtp no, look at this method https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.html#get(java.lang.String,%20java.lang.Class%3CT%3E). In your MainActivity it will give you the same ViewModel as in SplashActivity if these ViewModels of one class. `ViewModel viewModel = mViewModelStore.get(key);`. So, your ViewModel will contain in ViewModelStore as it was created earlier. – Nazarii Moshenskiy Dec 21 '18 at 10:44
  • @BVtp no, data will not be retained after activity destroying. You can use 1 viewmodel in multiple activities, it's legal, but `ViewModel` as an Arch component of Android doesn't provide a possibility to share its data. – Nazarii Moshenskiy Dec 21 '18 at 14:52
  • so basically there's no point in loading the data in splash ? which means there's no point in splash screen altogether? that's so weird.. how do I accomplish it then ? only by abandoning MVVM & livedata ? – BVtp Dec 21 '18 at 14:52
  • The idea of splash screen was to load some data and update DB or smth like this. So, you shouldn't ask to load data and finish splash screen before it done. So, probably you don't even need it. But this is only my opinion. – Nazarii Moshenskiy Dec 21 '18 at 15:16
  • I didn't want to finish splash before the call is done. I wanted to wait for the data to be completely fetched in splash activity, and only then transfer to main activity. – BVtp Dec 21 '18 at 15:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185623/discussion-between-bvtp-and-nazariy-moshenskiy). – BVtp Dec 21 '18 at 16:53