Parcels and inheritance in Android

To pass information between Activities and save states during the application lifecycle, Android uses Parcel objects. Like Serializable in Java, the Parcelable interface offers a marshalling mechanism to represent and transfer object instances as bytes. In this post I will present some of the differences between Parcels and Serializable, then explain how to implement the Parcelable interface correctly, in particular in a hierarchy of inherited classes. Why Parcel? Parcels are a lightweight alternative to Serializable for inter-process communication. As stated in the documentation, it is not a general purpose serialization mechanism and should not be used to store object representations…

read more...

A try-with-resources equivalent for Java 6 and Android

In Java core libraries, resources such as network, files and databases can be accessed through a large range of low-level interfaces which all require to explicitly handle exceptions and release resources properly. As a consequence, writing such code is arguably more error-prone than in languages with high-level resource access methods, and it is actually quite common to find bugs related to flawed resource management in Java codebases. To tackle this issue, Java 7 offers higher-level methods to access files in the java.nio.file package, but also a new try-with-resources language construct to automatically release resources, even when errors occur.…

read more...