airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

Sharing state inbetween activities #119

Closed jQrgen closed 5 years ago

jQrgen commented 5 years ago

Is it possible to share state inbetween activites? I get errors when trying to access a viewmodel in an activity.

qbait commented 5 years ago

I would be surprised if you can. MxRx relies on Jetpack ViewModel and Google recommends using a single Activity approach because you can share ViewModel.

gpeal commented 5 years ago

@jQrgen No, MvRx does not support this out of the box. If you want to build your own system or have a ViewModel on your Application/Dagger graph, you can do that as you see fit.

jQrgen commented 5 years ago

So if i use MvRxViewModel for keeping a global state such as the state of a user i should just initiate it in the fragment identified in nav_graph.xml's app:startDestination=""?

What do you guys use for approaching this? A viewModel initiated in MainActivity?

gpeal commented 5 years ago

@jQrgen Well if you want it to persist across activities, you can't instantiate it in a Fragment. You can try having one in your dagger/DI graph with a @Singleton scope or something.