一、前言
1.Jetpack学习之DataBinding
2.Jetpack学习之Lifecycles
通过上两篇的学习,对DataBidning和Lifecyces有了简单的了解。实际使用中都是结合 LiveData 和 ViewModel 一起使用的。
以下结合代码简单使用学习下,这里也是记录下学习的过程。
二、LiveData
先来简单看下LiveData这个类,和其中的几个方法:
public abstract class LiveData<T> {
protected void setValue(T value)
public T getValue()
protected void postValue(T value)
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer)
}
这里列出了几个关键的方法,自己可以在AS中查看详细的源码。
从上述代码上看出,LiveData是抽象类,实际使用中,一般使用MutableLiveData
public class MutableLiveData<T> extends LiveData<T> {
@Override
public void postValue(T value) {
super.postValue(value);
}
@Override
public void setValue(T value) {
super.setValue(value);
}
}
MutableLiveData 类也是非常的简单,从泛型也可以看出,这个就是我们需要传入的自己业务数据类。
LiveData先简单了解下,下面再来看下ViewModel
三、ViewModel
简单看下ViewModel类
public abstract class ViewModel {
protected void onCleared()
}
这里没有完整的copy源码里面的代码,只是把关键的方法写出来了,后面会介绍这个方法。
ViewModel 也是抽象类,实际使用中,我们需要继承ViewModel类。也可以选择继承AndroidViewModel类:
public class AndroidViewModel extends ViewModel {
@SuppressLint("StaticFieldLeak")
private Application mApplication;
public AndroidViewModel(@NonNull Application application) {
mApplication = application;
}
/**
* Return the application.
*/
@SuppressWarnings({"TypeParameterUnusedInFormals", "unchecked"})
@NonNull
public <T extends Application> T getApplication() {
return (T) mApplication;
}
}
AndroidViewModel类继承自ViewModel类,初始化的时候传入了application对象,这在涉及到使用application对象时可以直接使用,当然,我们也可以在自定义的Application类中提供一个获取全局Application对象的方法。
四.使用LiveData和ViewModel
通过下面简单的的例子来看下使用,首先自定义MainViewModel继承ViewModel,声明需要使用的数据:
public class MainViewModel extends ViewModel {
// 用户名
public MutableLiveData<String> mName = new MutableLiveData<>();
// 密码
public MutableLiveData<String> mPwd = new MutableLiveData<>();
public void submit() {
String name = mName.getValue();
String pwd = mPwd.getValue();
Log.i("viewmodel", "用户名:" + name + ",密码:" + pwd + ",提交成功");
}
@Override
protected void onCleared() {
super.onCleared();
// 清除
}
}
接着在MainActivity中获取MainViewModel对象,注意,这里不是直接new MainViewModel
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mBinding;
private MainViewModel mainViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// 关联生命周期
mBinding.setLifecycleOwner(this);
// 获取MainViewModel实例
mainViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
}
}
这里很奇怪,直接 new 多省事,为啥非要这样,这里简单分析下,
从名字上分析 ViewModelProvider , ViewModel 提供者,构造方法需要传入 ViewModelStoreOwner 对象,这里传入 this,相当于MainActivity是实现了这个接口,另一个参数是工厂接口,用于创建ViewModel的工厂
public static class NewInstanceFactory implements Factory {
@SuppressWarnings("ClassNewInstance")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
//noinspection TryWithIdenticalCatches
try {
return modelClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
}
}
}
NewInstanceFactory 的代码也是非常简单,实现 create 方法,通过反射无参构造方法创建类对象
public interface ViewModelStoreOwner {
/**
* Returns owned {@link ViewModelStore}
*
* @return a {@code ViewModelStore}
*/
@NonNull
ViewModelStore getViewModelStore();
}
ViewModelStoreOwner 是一个接口,里面只有一个方法,获取ViewModelStore。
看下是哪个父类实现了这个接口
public class ComponentActivity extends androidx.core.app.ComponentActivity implements
LifecycleOwner,
ViewModelStoreOwner,
HasDefaultViewModelProviderFactory,
SavedStateRegistryOwner,
OnBackPressedDispatcherOwner{
@NonNull
@Override
public ViewModelStore getViewModelStore() {
if (getApplication() == null) {
throw new IllegalStateException("Your activity is not yet attached to the "
+ "Application instance. You can't request ViewModel before onCreate call.");
}
if (mViewModelStore == null) {
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
// Restore the ViewModelStore from NonConfigurationInstances
mViewModelStore = nc.viewModelStore;
}
if (mViewModelStore == null) {
mViewModelStore = new ViewModelStore();
}
}
return mViewModelStore;
}
}
从上述代码来看,getViewModelStore的时候,创建一个ViewModelStore类,看下ViewModelStore类,这个类比较简单:
public class ViewModelStore {
private final HashMap<String, ViewModel> mMap = new HashMap<>();
final void put(String key, ViewModel viewModel) {
ViewModel oldViewModel = mMap.put(key, viewModel);
if (oldViewModel != null) {
oldViewModel.onCleared();
}
}
final ViewModel get(String key) {
return mMap.get(key);
}
Set<String> keys() {
return new HashSet<>(mMap.keySet());
}
/**
* Clears internal storage and notifies ViewModels that they are no longer used.
*/
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.clear();
}
mMap.clear();
}
}
从上述代码可以看下ViewModelStore 中使用 HashMap来保存创建的ViewModel实例,再回过头看下 ViewModelProvider 的 get 方法
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
String canonicalName = modelClass.getCanonicalName();
if (canonicalName == null) {
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
}
return get(DEFAULT_KEY + ":" + canonicalName, modelClass);
}
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
ViewModel viewModel = mViewModelStore.get(key);
if (modelClass.isInstance(viewModel)) {
//noinspection unchecked
return (T) viewModel;
} else {
//noinspection StatementWithEmptyBody
if (viewModel != null) {
// TODO: log a warning.
}
}
if (mFactory instanceof KeyedFactory) {
viewModel = ((KeyedFactory) (mFactory)).create(key, modelClass);
} else {
// 第一次 使用 工厂 create 方法来创建 viewModel 实例
viewModel = (mFactory).create(modelClass);
}
// 将viewModel保存到 HashMap中
mViewModelStore.put(key, viewModel);
//noinspection unchecked
return (T) viewModel;
}
上述代码中,可以看到,当我们使用自定义 MainViewModel 时,通过 ViewModelProvider 的 get 方法来获取实例,其中的工厂方法通过反射创建实例,将实例保存到 mViewModelStore 对象中,这里的
mViewModelStore 就是 ComponentActivity 中的 mViewModelStore 。
简单的流程分析就是这样,刚才列出的 ViewModel中有 onCleared(),这个方法会在Activity销毁的时候自动调用,可以在这里做资源的释放相关的工作。这个是在哪调用的,也来简单分析下:我们知道创建ViewModel 时候 viewModel 的实例会被保存到 ViewModelStore 的 HashMap 中,在ViewModelStore 中有个 clear 方法会遍历 map ,逐个调用 viewmodel 中的 clear() 方法,这时候在 ViewModelStore 的 clear 方法打个断点来看下:
看到这里,是不是就明白了,ComponentActivity在创建的时候,会在生命周期组件中添加观察者,当生命事件是销毁时,调用 viewModelStore中的 clear 方法。
这里我们通过一个简单的例子来使用下:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="vm"
type="com.learn.mvvmtest.MainViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:text="@={vm.mName}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:text="@={vm.mPwd}" />
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
</layout>
声明数据MainViewModel,双向绑定用户名和密码输入
MainActivity 代码:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mBinding;
private MainViewModel mainViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
mBinding.setVm(mainViewModel); // 设置 vm
mBinding.setLifecycleOwner(this); // 关联生命周期
mBinding.submitBtn.setOnClickListener(v -> mainViewModel.submit());
}
}
使用DataBinding 绑定布局,ViewModelProvider获取MainViewModel对象,设置VM,关联生命周期,这里点击提交的时候获取用户名和密码
public void submit() {
String name = mName.getValue();
String pwd = mPwd.getValue();
Log.i("viewmodel", "用户名:" + name + ",密码:" + pwd + ",提交成功");
}
因为是双向绑定,界面有变化时,数据对应改变,所以很方便的获取到了用户输入的数据,代码是不是很简洁。
五、后记
这里从个人理解学习,简单分析和使用了下 LiveData和ViewModel。
因能力有限,有些地方写的可能不对,欢迎各位评论指出。