0

I have a class/object which I'm attempting to make Parcelable so I can save the object to a Bundle (Well, in fact, I will add the object to an Arraylist and then save this list to, and restore it from a Bundle, complete with the objects held within it).

I was doing this by making the class the objects are derived from Serializable.

Someone advised making the class Parcelable instead. However, I can't implement it because one of the properties of this class is a FloatBuffer, and I can't see a method to save this to the Bundle in a Parcelable class.

Is there anyway to do this? Or am I out of luck?

Zippy
  • 3,826
  • 5
  • 43
  • 96

1 Answers1

0

Parcel class has methods called writeFloatArray(float[]), readFloatArray(float[]) and createFloatArray(), so you can use them to store and retrieve float arrays.
Now, I didn't use FloatBuffer, but it looks like it has method FloatBuffer#array() which returns the array which backs this buffer. I think you can get the array from the buffer and store it in the parcel.
Later, when you need to unparcel your data, you can retrieve the float array you previously stored here and put it inside the new FloatBuffer instance by FloatBuffer#put(float[], int, int) method.

Edit: If the array() method doesn't give you the desired result, you can try the following:

final int size = buffer.remaining();
final float[] floatsArray = new float[size];
buffer.get(floatsArray); 
// now floatsArray contains the values from buffer,
// you can safely put it into the Parcel.
aga
  • 27,954
  • 13
  • 86
  • 121
  • Hi @Aga thank you for your answer. I've tried what you suggest (if I'm understanding you correctly), when writing to the Bundle, I've done dest.writeFloatArray(myFloatBuffer.array()); When I run the code and press the home key (to invoke saving to the bundle), it throws a 'UnsupportedOperationException' at java.nio.ByteBufferAsFloatBuffer' on the above line. Do you have any other ideas? Thanks – Zippy Mar 13 '15 at 19:44