Android---AndroidViewModel

AndroidViewModel是系统提供的一个类,是ViewMode的一个子类。

这一篇将来介绍下用AndroidViewModel。做一个简单的sp数值保存操作。在我们关掉程序后,重新打开时数值可以重新呈现

因为SharedPreferences是继承Context的一个接口。所以得到对象需要context。

AndroidViewModel:

public class MyViewModel extends AndroidViewModel {

    SavedStateHandle handle;
    String key = getApplication().getResources().getString(R.string.data_key);
    String spName = getApplication().getResources().getString(R.string.sp_name);

    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        if (!handle.contains(key)) {
            load();
        }
    }

    public LiveData<Integer> getNumber() {
        return handle.getLiveData(key);
    }

    private void load() {
        SharedPreferences sp = getApplication().getSharedPreferences(spName, Context.MODE_PRIVATE);
        int x = sp.getInt(key, 0);
        handle.set(key, x);
    }

    public void save() {
        SharedPreferences sp = getApplication().getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, getNumber().getValue());
        edit.apply();
    }

    public void add(int x) {
        handle.set(key, getNumber().getValue() + x);
    }
}


java:

    private MyViewModel viewModel;
    private ActivityViewModelTestBinding bindingBinding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindingBinding= DataBindingUtil.setContentView(this, R.layout.activity_view_model_test);
        viewModel=new ViewModelProvider(this).get(MyViewModel.class);
        bindingBinding.setData(viewModel);//.setXxx(yyy)。Xxx表示在布局文件data标签的name
        bindingBinding.setLifecycleOwner(this);//如果没有这行,数据的改变不会被观察到
    }

    @Override
    protected void onPause() {
        super.onPause();
        viewModel.save();
    }

xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="data"
            type="com.example.viewmodeltest.MyViewModel" />
    data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(data.getNumber)}"/>

        <Button
            android:id="@+id/btn_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{()->data.add(1)}"
            android:text="@string/add"/>

        <Button
            android:id="@+id/btn_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{()->data.add(-1)}"
            android:text="@string/sub"/>
    LinearLayout>
layout>

这里需要思考一个问题:
在上面AdnroidViewModel中,在哪里执行这个save方法是比较好的?

你可能感兴趣的:(Android)