android-viewmodel

Android Architecture Components: ViewModel/Repository vs bind to Service/IntentService

旧时模样 提交于 2019-12-05 09:53:50
I want to implement/refactor an app to the Android Architecture Components concept, see https://developer.android.com/jetpack/docs/guide In this topic Android Architecture Components ViewModel - communication with Service/IntentService , I found a very good answer to the architectural issue, see https://stackoverflow.com/a/46736146/6251815 But i want to ask, how to bind the service from a repository, because we have neither context nor activity here. To be clear on that, the question is about how to merge both concepts. What is my situation? I need to have a boundService (see https://developer

Android ViewModel recreated on screen rotation

◇◆丶佛笑我妖孽 提交于 2019-12-04 22:49:02
问题 I found a case when architecture components ViewModel isn't retained - in short it goes as follows: Activity is started and ViewModel instance is created Activity is put to background Device screen is rotated Activity is put back to foreground ViewModel's onCleared method is called and new object is created Is it normal behavior of Android that my ViewModel instance is getting destroyed in this case? If so, is there any recommended solution of keeping its state? One way I can think of is

ViewModelProviders is deprecated in 1.1.0

一世执手 提交于 2019-12-04 22:15:59
Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel : val model = ViewModelProviders.of(this).get(MyViewModel::class.java) When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there is no such class ViewModelProviders . Going to the documentation for ViewModelProviders , I saw a comment saying: This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the

How to generate composed view models with temporary sub views?

旧时模样 提交于 2019-12-04 15:29:40
Scenario I have a quizz generator, which generates a sequence of quizzes of different classes. The sequence is of unlimited length. There is a view model for the quizz generator. There is a view model for each type of a quizz. The quizz generator view model should create the view models of the quizzes depending on their classes. Issue A view model must not hold a reference to the lifecycle, but I need the lifecycle to create view models. ViewModelProviders.of(lifecycle).get(classForQuizzType); Questions Where do I create the sub view models of the quizzes? The one solution I can think of is to

Android ViewModel inside Service (Alternative)

丶灬走出姿态 提交于 2019-12-04 10:32:12
问题 I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ViewModelProviders.of(this).get(MyModelviewModel.class); But as you know this can be only AppCompat or Fragment Is there some alternative? or can I put observer directly on my LiveData like Im puting on ViewModel viewModel.getList().observe(Playground.this, new Observer<List<TestEntity>>() { @Override public

When is the viewmodel onCleared called

旧巷老猫 提交于 2019-12-03 14:37:25
问题 Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ? A situation: Activity1+viewModel1--->(rotation)--->Activity1+viewModel1 --->(launch Intent)--->Activity2+viewModel1 is this sharing possible and is it a good practice. Also, since the app lifecycle callbacks, onPause->onStop->onDestroy is same for both 1.activity rotating

Android ViewModel recreated on screen rotation

穿精又带淫゛_ 提交于 2019-12-03 14:18:33
I found a case when architecture components ViewModel isn't retained - in short it goes as follows: Activity is started and ViewModel instance is created Activity is put to background Device screen is rotated Activity is put back to foreground ViewModel's onCleared method is called and new object is created Is it normal behavior of Android that my ViewModel instance is getting destroyed in this case? If so, is there any recommended solution of keeping its state? One way I can think of is saving it once onCleared is called, however, it would also persist the state whenever activity is actually

Should I include LifecycleOwner in ViewModel?

旧时模样 提交于 2019-12-03 12:42:39
问题 LifecycleOwner is currently needed in order for me to create an observer. I have code which creates an Observer in the ViewModel so I attach the LifecycleOwner when retrieving the ViewModel in my Fragment. According to Google's documentation. Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context. Did I break that warning and If I did, what way do you recommend me to move my creation of an observer for data return? I only

Android ViewModel inside Service (Alternative)

若如初见. 提交于 2019-12-03 07:08:44
I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ViewModelProviders.of(this).get(MyModelviewModel.class); But as you know this can be only AppCompat or Fragment Is there some alternative? or can I put observer directly on my LiveData like Im puting on ViewModel viewModel.getList().observe(Playground.this, new Observer<List<TestEntity>>() { @Override public void onChanged(@Nullable List<TestEntity> items) { recyclerViewAdapter.addItems(items); } }); LiveData

Observing LiveData from ViewModel

家住魔仙堡 提交于 2019-12-03 05:28:29
问题 I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in order to get said value, I need to observe the LiveData object returned from my data fetching class. The observe method required a LifecycleOwner object as the first parameter, but I obviously don't have that inside of my ViewModel and I know I am not