如何在 Bundle 中存储 SparseArray<Double>?

How to store SparseArray<Double> in Bundle?

SparseArray 如何存储在一个 bundle 中?

public void onSaveInstanceState(Bundle savedState) {
    // ...
    SparseArray<Double> values = mAdapter.getValues();
    savedState.putSparseParcelableArray(values);

上面的代码导致编译错误

"SparseArray< ? extends Parcelable > in Bundle cannot be applied to SparseArray< Double >".

如何解决?

为了保存 SparseArray,将此数据放入列表中并将该列表保存到 outState 包中。

然后在 onRestoreInstanceState 中,检索列表并将其再次转换为 SparseArray。

for detailed explaination

根据 docs SparseArray<? extends Parcelable> value) mean The class and it's sub type is allowed but Double 没有实现 Parcelable 所以你不能将它与 bundle persistence 一起使用,尽管你可以让你的自定义 bean class 对象实现 parcelable