Hello guys. We are new to AAC. We are wondering ho...
# android
Hello guys. We are new to AAC. We are wondering how we should or where we should keep the state of our Auth. At first glance we thought of having a central AuthViewModel, but we have few activities and the view model couldn't be share as far as I know. Now we are thinking on keeping the state in the AuthRepository. Could anyone share experiences on it? Regards!
You can share viewmodels across activities if you provide your own ViewModelStore. Before doing that though, consider the Android source of UI truths below in terms of increasing fragility: Application, Service, Activity, Fragment, Views Therefore at any level, you can provide your own custom
, but as to where will it be scoped, it’ll be likely either at Application level (e.g singletons) or Service level if you can’t use activities. Which for simplicity and maintenance sake, you might just as well use a singleton for that.
thanks @Amirul Zin, looking into it
is the way to go. Either inject it directly into
(or add another layer and call
that access the
So keeping the state int the Repository? isn't that opposed to AAC idea of keeping state in VM?
General rule I follow is to retrieve state from ViewModel (ideally common-state from LiveData last emitted item) if it exists, otherwise retrieve state from the repository (either network or local cache). Take a look at https://github.com/igorwojda/Android-Showcase
@igor.wojda thanks mate, cool project, already got some ideas from it 🙂 . Another question you might know somehow, is it ok to have view models per custom views? so far we wanted but it seems not the kind of things we should do. Kind regards!
Thx 🙂 Never had need for view VM, but AFAK they are designed to work only pre Activity or Fragments
👍 1