-
Notifications
You must be signed in to change notification settings - Fork 0
Best Practices
mdsimmo edited this page Jun 30, 2018
·
2 revisions
- When restoring data, NEVER read the data fields/methods of an object read from
DataIn. The returned object only has an instance id but may not exist yet. If you need the fields of another object when restoring the object, save those fields in the bundle or use lazy initialisation. - Never use a key starting with "__" in a bundle. Undefined behaviour will result.
- Never save inner classes or anonymous classes. Even if these classes implement
DataSerializable, their constructor secretly adds the reference to the outer class and they store extra data that is lost through the save/restore process. An exception will be thrown on restore should this be done.
- Be careful of lists. Even though lists are supported, it is common to initialise a list as follows:
The above is not an
new ArrayList<Integer>(){{ add(1); add(2); }}
ArrayList, it is an anonymous class that extends anArrayList. Therefore, it cannot be saved. If it is unknown where a list comes from, it is recommended to first copy the list to a new list/array before storing it in the bundle.