Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Proper way of sending large amount of data from View to ViewModel

+6
−0

The problem

I am working on a simple drawing app for Android. I have a custom view on which the user can draw. This means that a lot of data (a list of points) is generated in this View.

I want to store this data in the ViewModel.

If I need to update a View after something changes in the ViewModel, I can just use LiveData in the ViewModel and put an observer in the View. However, what I need is the other direction: changes in the View should lead to an update of the data in the ViewModel.

What I've learnt so far

I can use MutableLiveData for this, however, it is unclear to me what the right way is to give the View access to the MutableLiveData.

I can access a ViewModel from an Activity, by calling:

viewModel = new ViewModelProvider(this).get(MyViewModel.class);

But, I cannot access the ViewModel from the View. ViewModelProvider takes a ViewModelStoreOwner as its parameter. An Activity is a ViewModelStoreOwner, but a View is not.
I believe this is intentional, otherwise the View would be a ViewModelStoreOwner itself. I could pass a reference to the Activity or the ViewModel itself to the View, but this feels wrong.

It seems that I need to use MutableLiveData. I could then use setValue or postValue on the MutableLiveData whenever the View has more data to send.
However, that just shifts the problem: how should the View get access to the MutableLiveData?

What I've tried

For now, I have a workaround.
I have created an interface ListOfPoints that is implemented by the Activity. The View has a method setPointsOwner(ListOfPoints) . So, effectively, the View still gets a reference to the Activity, but can only use it to manipulate the data. The implementation of the interface, inside the Activity, then updates the MutableLiveData it gets from the ViewModel.

This works, but still feels wrong.

The question

How can I send a lot of data from a View to a ViewModel?

Is it proper coding style to give the View access to MutableLiveData from the owning Activity?
Or is there a way in which I can make the ViewModel observe the View, without letting it have a direct reference to the View?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (3 comments)

1 answer

+3
−0

I agree with you in that using MutableLiveData for achieving this feels wrong. I think this is because:

  • LiveData is meant for sending data to LifecycleOwners such as Activity or Fragment, abstracting the lifecycle away, and your workaround sends data in the opposite direction.
  • as a result, if your View happens to send data by through MutableLiveData while the lifecycle is stopped or paused, any callback you attach on the MutableLiveData on the ViewModel side simply will not run, which is probably not what you would expect.
  • afaik Views are not LifecycleOwners in the first place.

What I've seen in most projects when they want to react to a Button click is that the Activity sets a View.OnClickListener as an anonymous class:

class MyActivity: Activity() {

    private lateinit var viewModel: MyViewModel

    override fun onCreate(...) {
        super.onCreate(...)
        
        viewModel = ....
    
        myButton.setOnClickListener {
             viewModel.myActionTriggered(...)
        }
    }
}

So to me, it makes sense to do something along those lines in your case too:

class MyActivity: Activity() {

    private lateinit var viewModel: MyViewModel

    override fun onCreate(...) {
        super.onCreate(...)
        
        viewModel = ....
    
        myCanvas.setListOfPointsListener { points ->
             viewModel.onListOfPointsChanged(points)
        }
    }
}

This way:

  • neither the Activity nor the ViewModel need to implement the ad-hoc interface
  • the ViewModel doesn't need to expose anything "mutable"
  • the LiveData get data out of the ViewModel to the Activity as it should, not the other way around
  • the ViewModel can still get the data from the View even when the lifecycle is "off", and it still can change its internal state in this case (other than posting to other LiveData, which would not work until the lifecycle is back "on")

A little off-topic comment: I think the name of ListOfPoints does not reflect what your interface does does. Your Activity doesn't become a "list of points", but rather a list of points listener. It doesn't contain the points itself, it just does something whenever that list changes.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Thanks, this makes sense! I'll look into it more closely later. Meanwhile you've got my vote. Good po... (1 comment)

Sign up to answer this question »