home.

tagged: android-lifecycle

Android: Live Data

Live data is a way for you to observe changes to data. Let's use it in a ViewModel:

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
    public MutableLiveData<String> text = new MutableLiveData<>();

    public LiveData<String> getText() {
        return text;
    }

    public void setText(String text) {
        this.text.setValue(text);
    }
}

Instead of having a plain String we have a MutableLiveData object. And when we want to update data we use setValue.
 

getText() returns a LiveData object. Let's see how we use that in our activity or fragment:

MyViewModel vm = ViewModelProviders.of(this).get(MyViewModel.class);

vm.getText().observe(this, new Observer<String>() {
    @Override
    public void onChanged(@Nullable String s) {
        ((TextView)findViewById(R.id.aTextView)).setText(s);
    }
});

We observe it. So when it changes we update our view.

android android-lifecycle


Android: View models

Android will save the state of a simple POJO between rotations if you use something called a View Model.
 
Let's first import it in our gradle file:

implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:1.1.0"

Then create a POJO that extends ViewModel:

import android.arch.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
    public String text = "";

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

And then in our activity, you can initalise it:

MyViewModel vm = ViewModelProviders.of(activityOrFragment).get(MyViewModel.class);

Now anything you save in that POJO will be retained until the activity is destroyed.
 
We can't pass the POJOs constructors arguments, however. But you can give ViewModelProviders.of second factory argument.

MyViewModelFactory factory = new MyViewModelFactory("Develop");
final MyViewModel vm = ViewModelProviders.of(this, factory).get(MyViewModel.class);

This factory is responsible for creating the view models. And we can pass that a constructor argument. And we use that to give the view model a constructor argument:

public class MyViewModelFactory implements ViewModelProvider.Factory {

    private final String appVersion;

    public MyViewModelFactory(String appVersion) {
        this.appVersion = appVersion;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if(modelClass.isAssignableFrom(MyViewModel.class)) {
            return (T) new MyViewModel(appVersion);
        }
        throw new IllegalArgumentException("Not found that view model, sunshine.");
    }
}

android android-lifecycle

Page 1 of 1