ViewModel分析

ViewModel的主要作用:
1、当界面横竖屏切换的时候,如果页面中有状态需要保存,那么可以通过ViewModel来保存状态。
2、防止内存泄露,主要是防止Activity或者Fragment的引用,在页面销毁时,还没回收,主要的应用场景是网络请求时候,当请求还没结束,但是请求的页面已经关闭,那么可能导致内存泄露。
3、数据共享,同一个Activity 的Fragment和Fragment之间,以及Fragment和Activity之间实现数据的实时更新。
4、分离了页面和业务逻辑的代码,就像mvp、mvvm框架。

使用:

项目的gradle中配置

//引入AndroidX吧,替换掉support包
   implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-rc03'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha02'  

构建一个User实体。

open class User(var age: Int, var name: String) : Serializable {
    override fun toString(): String {
        return "User{" + "age = " + age + "name = " + name + "}"
    }
}

构建UserModel,继承制ViewModel

class UserModel :ViewModel(){


    val saveData = MutableLiveData<User>().apply {
        value = User(10,"xiaoming")
    }

    val firstData = MutableLiveData<String>().apply {
        value =  "第一个"
    }


    fun get():MutableLiveData<User>{
        load()
        return  saveData
    }

    fun load(){
        saveData.postValue(User(10,"xiaoming"))
    }

    fun dosomething(){
        var user:User ?= saveData.value
        if(null != user){
            user.name = "lili"
            user.age = 10
            saveData.value = user
        }
    }
    fun dosomething2(){
        firstData.value = "sss"
    }

}

在MainActivity中

class MainActivity : AppCompatActivity() {

    lateinit var showTitle: TextView;
    lateinit var userModel: UserModel ;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showTitle = findViewById(R.id.tv_onclic)
         userModel = ViewModelProviders.of(this).get(UserModel::class.java)
        userModel. saveData.observe(this, object : Observer<User> {
            override fun onChanged(t: User?) {
                showTitle.setText("---" + t.toString())
            }
        })
        findViewById<TextView>(R.id.bt_onclick).setOnClickListener {
            userModel.dosomething()
        }

借助LiveData,总共做了俩件事,第一件是读取 User对象,第二件是更改User的属性,基本使用就是这样了。
当正常安装点击按钮后,再切换横竖屏,发现对象的属性还是没有用的切换之前的属性。说明页面已经和数据分离开来。类似做了onRestoreInstanceState(),好处是比onRestoreInstanceState()存储的数据大,以及不用对数据进行序列化和反序列化。

源码分析:

首先看入口ViewModelProviders.of(this).get(UserModel::class.java)获取到ViewModel对象。
再通过LiveData来更新数据变化

 userModel.saveData.observe(this, object : Observer<User> {
            override fun onChanged(t: User?) {
                showTitle.setText("---" + t.toString())
            }

        })

先看ViewModelProviders.of(this)方法,最后都会调用到

 @NonNull
    @MainThread
    public static ViewModelProvider of(@NonNull Fragment fragment, @Nullable Factory factory) {
        //检查application
        Application application = checkApplication(checkActivity(fragment));
        if (factory == null) {
  // 后面通过factory来生成需要的ViewModel
            factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
        }
        return new ViewModelProvider(fragment.getViewModelStore(), factory);
    }

这里有个 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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容