1.首先在ViewModel中创建SavedStateHandle保存数据

public class MainViewModelLiveData extends ViewModel {
    private SavedStateHandle mSavedStateHandle;

    public MainViewModelLiveData(SavedStateHandle savedStateHandle) {
        mSavedStateHandle = savedStateHandle;
    }

    public MutableLiveData<Integer> getmSavedStateHandle() {
        if (!mSavedStateHandle.contains(StringUtil.KEY)) {
            mSavedStateHandle.set(StringUtil.KEY, 0);
        }
        return mSavedStateHandle.getLiveData(StringUtil.KEY);
    }

    public void addNum() {
        getmSavedStateHandle().setValue(getmSavedStateHandle().getValue() + 1);
    }
}

2.然后在Activity中实例化和绑定参数

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private MainViewModelLiveData mMainViewModelLiveData;
    private ActivityMainBinding mActivityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mMainViewModelLiveData = new ViewModelProvider(this).get(MainViewModelLiveData.class);
        mActivityMainBinding.setData(mMainViewModelLiveData);
        mActivityMainBinding.setLifecycleOwner(this);
    }
}

3.页面

<?xml version="1.0" encoding="utf-8"?>
<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.mylivedata.MainViewModelLiveData" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(data.getmSavedStateHandle)}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{()->data.addNum()}"
            android:text="Button"
            app:layout_constraintBottom_toTopOf="@+id/textView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

4.String工具类

public class StringUtil {
    public static final String KEY = "key";
}
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐