简体   繁体   中英

Handling input with Android Architecture Components

TL;DR

How do I deal with Activities that actively change data (for example through an EditText)? Do I keep saving their state in the SavedInstanceState on rotation and only use the ViewModel when all of the fields are ready, or is there a way to make the ViewModel responsible for checking/holding/using the UI's data?

Question

I'm developing my application using Google's Arch. Components, and writing my latest class I've noticed I'm not really sure on what the best practice is when handling, say, data coming from an Activity form.

Example

I have a POJO made of title, description, location, type

I have an Activity with four EditText: title_et , description_et , location_et , type_et .

My ViewModel, through a Repository (irrelevant here), can send an object to the Database when the sendObject function is called.

How I'm doing it now

The activity has the mTitle , mDescription , mLocation , mType . On rotation, the activity saves all of the EditText values in the savedInstanceState bundle, and it loads them again populating the views.

When the user wants to send the object, it clicks a button and the activity calls the function viewModel.sendObject(mTitle, mDescription, mLocation, mType) after the necessary checks.

Problems with this approach

The activity is responsible of holding/checking all the data of the EditTexts, basically making the ViewModel only responsible of interacting with the Repository.

What I'd like to accomplish

Ideally, I'd want to make the Activity only responsible of the UI, delegating everything to the ViewModel.

This way I could call sendObject() and the ViewModel would have already all of the data needed.

The LiveData situation

Right now the ViewModel has only one instance of LiveData, inside that there is a Resource (which is taken from here ) and it's used to "tell" the Activity that new data has arrived or an error occurred.

This approach works fine in all Activities that just receive data from the network and display them. What do I do when I want to synchronise data coming FROM the Activity? Do I use one LiveData for each field and use that to display potential errors?

I've read most of the samples but all of the Activities there are passive.

Conclusion

Thanks to anyone who takes the time to help.

您可以将逻辑分离为模型字符串类,另一个包含编辑文本字段的所有字符串值的类只是在类的顶部分配字符串值。

You can have a LiveData of your model in the ViewModel and alter it from the View (Activity/UI). The downside is that to update the LiveData, you need to copy whole Model, edit it and post it back to live data.

The second way is to dissect Model's components in the ViewModel into individual parameter LiveDatas. Later when form is submitted you can reconstruct the Model.

What you can do for native fields is use data binding. For other you need manually update LiveData from the View with listeners etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM