Reputation: 254
I don't know how to save the object of bean class in onSavedInstance and retrieve it back in onCreate by checking savedInstanceState as null.
So far I've been trying this code but not able to either save/retrieve the values:-
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();
}
}
}
What should I do to save my bitmaps and the object of Book so that it doesn't make new instance of the class on orientation change?
Upvotes: 1
Views: 6959
Reputation: 22306
You can save them as Parcelable ArrayLists
savedInstanceState.putParcelableArrayList("bookThumb", bookData);
savedInstanceState.putParcelableArrayList("bookBitmaps", bitmaps);
savedInstanceState.putParcelableArrayList("featureBook", bookIds);
Your Book
and Feature
classes will need to implement Parcelable
for this to work, though. Bitmap is already a parcelable class
Upvotes: 2