ViewModel Android architectural components of learning

Overview

Today in the learning process, I learned about the architectural components of the Android ViewModel learning operation. ViewModel is part of our Android Jetpack part of the wind.
Android developer documentation is to say:

ViewModel class is intended to focus on the life cycle of the way storage and management of interface-related data. ViewModel class so that data can continue to exist after the rotation and other configuration changes occur.


Take a look at how we use the ViewModel

Architectural components provide the interface controller ViewModel
helper class, which is responsible for preparing the data for the interface will automatically keep ViewModel objects during configuration changes, so that they can immediately provide data storage to the next Activity or Fragment instance. If you need to display a list of users in the application, make sure to obtain and retain the responsibilities assigned to the user list ViewModel, instead of Activity or Fragment.

Then we follow our international practice, we look at the implementation code:

package com.example.viewmodellearn;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

public class MyViewModel extends ViewModel {
    private MutableLiveData<List<User>> users;

    public LiveData<List<User>> getUsers() {
        if (users == null) {
            users = new MutableLiveData<List<User>>();
            loadUsers();
        }

        return users;
    }

    private void loadUsers() {
        List<User> usersList=new ArrayList<>();
        usersList.add(new User("Jia Hao","Male","18","China JiangSu"));
        users.setValue(usersList);
    }
}

First we need to create a class name MyViewModeland the class needs to inherit from our ViewModelso we created complete.
Here I hope ViewModelto help me deal with User information, because as a software, User information is quite important.
But before that we need to create a User class of entity. This casually hold code.

After these we started to build up our ViewModel.

  • Create an MutableLiveData<List<User>>object for the users, the users relatively important, as we will often use.
  • Then we need a getUsers method, this method can get help with the operation of Users. The method returns a value of several User
  • But when we get a point to note is that if we have no direct access to data stored in the user then null pointer error occurs, the problem is still very sick. So! Need to determine whether we get in front of users is empty, if empty if we need to initialize our User data is our loadUsers method.

So that our ViewModel OK.

After this ViewModel complete look at how our Activity or Fragment to operate.
The old rules look at our code:

package com.example.viewmodellearn;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        MyViewModel model= ViewModelProviders.of(this).get(MyViewModel.class);
        model.getUsers().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                ((TextView)findViewById(R.id.main_Txt_UserName)).setText(users.get(0).getName());
            }
        });
    }
}

The operations here are my interface has been operating a TextView assignment.

  • We have just completed the ViewModel is instantiated, then we can update the UI to complete our operations through our getUsers method.

If you re-create the change Activity, then MyViewModel instance he received the first instance of an Activity and create the same, when the Activity is completed, the mine construction will be called the onCleared ViewModel () method to clean up resources.

Guess you like

Origin www.cnblogs.com/cao-1/p/12636145.html