Skip to content

Best Practices

mdsimmo edited this page Jun 30, 2018 · 2 revisions

NEVER do the following:

  • 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.

Recommendations

  • Be careful of lists. Even though lists are supported, it is common to initialise a list as follows:
    new ArrayList<Integer>(){{ add(1); add(2); }}
    The above is not an ArrayList, it is an anonymous class that extends an ArrayList. 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.

Clone this wiki locally