how to use viewmodel singleton for activities?

故事扮演 提交于 2021-01-27 07:30:42

问题


MyApp need hold a User object in whole context,A,B,C activities'xml use this User object,when A edit User,I want B and C notifyChange,how to deal this problem with databinding,livedata and viewModel?

Formerly I make User.class extend BaseObservable,but POJO will be very troublesome and must not be a null,sometimes User maybe null such as not login. Now I Change to use LiveData, make Pojo simple and not extend BaseObservable,but when A edit,B and C not work,I think i need ABC use same viewModel instance in memory,but this will cause viewModel's onClear() trigger manytimes.


回答1:


Based on this part of the documentation: https://developer.android.com/topic/libraries/architecture/livedata#extend_livedata

The fact that LiveData objects are lifecycle-aware means that you can share them between multiple activities, fragments, and services. To keep the example simple, you can implement the LiveData class as a singleton as follows:

You can create a singleton for your view model like I did here:

    companion object{
        private lateinit var instance: ViewModelProfile
        
        @MainThread
        fun getInstance(userId: String): ViewModelProfile{
            instance = if(::instance.isInitialized) instance else ViewModelProfile(userId)
            return instance
        }
    }

Then I call it and get instance anywhere like this:

    val profileVModel = ViewModelProfile.getInstance(user.uid)



回答2:


Another way is to have one singleton repository to hold your user data and each viewModel can have that repository and share the same data between activities.




回答3:


If you want to share common ViewModel between ABC activities, then it is suggested to keep them as 3 fragments in a single Activity, create ViewModel of that Activity which can be shared among all three fragments A, B, and C.

Also what you are trying to achieve with activities is like this, suppose you have done some operation in activity A, if you want Activity B and C to get notified about them then they need to be running to get notified, which won't be happening, so instead you should use Intent or Bundle to pass needed information when the activity get started.

Updated

There are other ways as well to achieve similar kind of functionality like,

  • Event Bus
  • RxJava Subjects (refer this)
  • Android State by Evernote (refer this)

This will allow you to have application level access of state, which can be accessed by any Activity or Fragment



来源:https://stackoverflow.com/questions/53039274/how-to-use-viewmodel-singleton-for-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!