Well the object isn't actually serialized until it needs to be sent across processes.
On Sun, Jun 5, 2011 at 4:05 PM, Emanuel Moecklin <[email protected]>wrote: > Hi Dianne > > My custom serialization methods get called if I do the > serialization/deserialization myself by writing/reading the object to/from a > byte[]. > If the same object is written using putSerializable the methods are NOT > called. So my assumption is that the Bundle is never actually > serialized/deserialized but just kept in memory? > So Android IS doing something to stop my code from working (or rather it > doesn't do it although the docs don't say it's doing standard Java > serialization, that's just something I would expect it to do). > > Here's some sample code that will allow to follow my argumentation: > > *public class SerializationActivity extends Activity {* > * > * > * public static class PersistentObject implements Serializable {* > * private static final long serialVersionUID = -8258412687839520234L;* > * private long id = (long) (Math.random() * 100000);* > * private void writeObject(ObjectOutputStream out) throws IOException { > * > * out.defaultWriteObject();* > * System.out.println("writeObject() called for: " + id);* > * }* > * private void readObject(ObjectInputStream in) throws IOException, > ClassNotFoundException {* > * in.defaultReadObject();* > * System.out.println("readObject() called for: " + id);* > * }* > * private void alive() {* > * System.out.println("I'm alive and my id is: " + id);* > * }* > * }* > * * > * private PersistentObject putSerializable = new PersistentObject();* > * private PersistentObject serializedObject = new PersistentObject();* > * * > * @Override protected void onCreate(Bundle savedInstanceState) {* > * super.onCreate(savedInstanceState);* > * if (savedInstanceState!=null) {* > * putSerializable = (PersistentObject) > savedInstanceState.getSerializable("putSerializable");* > * putSerializable.alive();* > * serializedObject = (PersistentObject) deserializeObject((byte[]) > savedInstanceState.getSerializable("serializedObject"));* > * serializedObject.alive();* > * }* > * }* > * > * > * @Override protected void onSaveInstanceState(Bundle outState) {* > * super.onSaveInstanceState(outState);* > * ** **System.out.println("onSaveInstanceState() called");* > * putSerializable.alive();* > * outState.putSerializable("putSerializable", putSerializable);* > * serializedObject.alive();* > * outState.putSerializable("serializedObject", > serializeObject(serializedObject));* > * }* > * > * > * public byte[] serializeObject(Serializable s) {* > * try {* > * ByteArrayOutputStream baos = new ByteArrayOutputStream();* > * ObjectOutputStream oos = new ObjectOutputStream(baos);* > * oos.writeObject(s);* > * return baos.toByteArray();* > * } catch (IOException e) {* > * return null;* > * }* > * }* > * > * > * public Object deserializeObject(byte[] in) {* > * try {* > * ByteArrayInputStream bais = new ByteArrayInputStream(in);* > * ObjectInputStream ois = new ObjectInputStream(bais);* > * return ois.readObject();* > * } catch (Exception e) {* > * return null;* > * }* > * }* > *}* > > Cheers > Emanuel Moecklin > 1gravity LLC > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

