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
can be use independently without ViewModel
,you can use observeForever(Observer<T> observer)
, or observe(LifecycleOwner owner, Observer<T> observer)
while you provide a proper LifecycleOwner
instance, you can implement LifecycleOwner
in your service or view.
ViewModelProviders
just provides a cache of ViewModel
for each Fragment
or Activity
, you can create your ViewModel
directly by new MyModelviewModel()
.