gpt4 book ai didi

安卓 : Saving data in onSavedInstance

转载 作者:行者123 更新时间:2023-11-29 22:07:46 24 4
gpt4 key购买 nike

我不知道如何将 bean 类的对象保存在 onSavedInstance 中,并通过将 savedInstanceState 检查为 null 在 onCreate 中将其取回。

到目前为止,我一直在尝试这段代码,但无法保存/检索值:-

public class BookActivity extends Activity{

private ArrayList<Bitmap> bitmaps = null;
private ArrayList<Book> bookData = null; //Book is the bean class
private TextView bookTitle = null;
private ArrayList<Feature> bookIds = null; // Another bean class

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable("bookThumb", bookData);
savedInstanceState.putSerializable("bookBitmaps", bitmaps);
savedInstanceState.putSerializable("featureBook", bookIds);
super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.penguin_main);

if(savedInstanceState!=null){
bookData = (ArrayList<String>) savedInstanceState.getSerializable("bookThumb");
bookData = (ArrayList<String>) savedInstanceState.getSerializable("bookThumb");
bookData = (ArrayList<String>) savedInstanceState.getSerializable("bookThumb");
}
else{
Toast msg = Toast.makeText(getApplicationContext(), "Values Not Restored!!!", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
}
}

我应该怎么做才能保存我的位图和 Book 的对象,以便它不会在方向更改时创建类的新实例?

最佳答案

您可以将它们保存为 Parcelable ArrayLists

savedInstanceState.putParcelableArrayList("bookThumb", bookData);
savedInstanceState.putParcelableArrayList("bookBitmaps", bitmaps);
savedInstanceState.putParcelableArrayList("featureBook", bookIds);

不过,您的 BookFeature 类需要实现 Parcelable 才能工作。 Bitmap 已经是一个 parcelable 类

关于安卓 : Saving data in onSavedInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com