0

I have used LiveData and ViewModel example but i dont understand use of this feature because i can change value directly without use this feature even this is growing number of line in code by using observing code and same as in ViewModel by creating MutableLiveData.

below ViewModel Code public class FirstViewModel extends ViewModel {

    // Create a LiveData with a String
    public MutableLiveData<String> mCurrentName;


    public MutableLiveData<String> getCurrentName() {
        if (mCurrentName == null) {
            mCurrentName = new MutableLiveData<String>();
        }
        return mCurrentName;
    }


}

Using in Activity

public class MainActivity extends AppCompatActivity {
    private FirstViewModel mModel;
ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding=DataBindingUtil.setContentView(this,R.layout.activity_main);


        // Get the ViewModel.
        mModel= ViewModelProviders.of(this).get(FirstViewModel.class);


        // Create the observer which updates the UI.
        final Observer<String> nameObserver = new Observer<String>() {
            @Override
            public void onChanged(@Nullable final String newName) {
                // Update the UI, in this case, a TextView.
                mBinding.mNameTextView.setText(newName);
            }
        };

        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
        mModel.getCurrentName().observe(this, nameObserver);


       mBinding.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              String anotherName = mBinding.etField.getText().toString();
             mModel.getCurrentName().setValue(anotherName);
            }
        });

    }
}
4

3 回答 3

2

The ViewModel and LiveData android architecture components together help to create lifecycle aware applications.

ViewModel: ViewModel classes are often used to significantly segregate the view logic (present in Activity classes) from the business logic which is contained in the ViewModel classes. This segregation is a good architecture design and becomes very important while maintaining large projects.

LiveData: LiveData helps in implementing the Observer Observable pattern in a lifecycle aware manner.

In your case, it may seem trivial since you are only setting value for a TextView. However consider common scenarios like hitting an api to retrieve data, etc. In such cases, the ViewModel is responsible for providing the data to be displayed in the Activity, which when done with the help of LiveData can help avoid crashes by ensuring lifecycle awareness easily.

于 2018-07-26T06:33:51.883 回答
0

You can read about live data from here. It is like Observer that looks for changing of data and notify observers that observable object has changed

于 2018-07-26T06:10:04.113 回答
0

In simple words its make your life eazy as a programmer when we go into the details like activity/fragment lifecycle handling, displaying updated data and more importantly separating the presentation layer from business logic and to create a more well structured application. please find more details from here

于 2018-07-26T06:16:43.187 回答