Come on:
1. When to use (choose from) Parcelable vs. Serializable?
Using Serializable is easier and faster to implement. However, the performance is worse.
Using Parcelable spends a little more time to implement and is a bit more complex than Serializable. Despite this, using Parcelable performs better.
2. How to implement Parcelable and Serializable?
Below is an example of a class that implements Parcel.
import android.os.Parcel;
import android.os.Parcelable;
public class Cliente implements Parcelable {
private int codigo;
private String nome;
public Cliente(int codigo, String nome) {
this.codigo = codigo;
this.nome = nome;
}
private Cliente(Parcel p){
codigo = from.readInt();
nome = from.readString();
}
public static final Parcelable.Creator<Cliente>
CREATOR = new Parcelable.Creator<Cliente>() {
public Cliente createFromParcel(Parcel in) {
return new Cliente(in);
}
public Cliente[] newArray(int size) {
return new Cliente[size];
}
};
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(codigo);
dest.writeString(nome);
}
}
In the example, it was necessary to rewrite two methods: describeContents
and writeToParcel
. The first is an integer that identifies the class. The second one is responsible for serializing class information.
Another important thing is the static CREATOR attribute. All classes that implement Parcelable must have this attribute, since it is the one who joins the features of a DataInputStream and DataOutputStream to serialize and deserialize objects. Also note that it calls the Private Client constructor that receives a Parcel that allows us to read data from it and passing it to the attributes.
Passing data via Intent:
Cliente cliente = new Cliente(1, "Glauber");
Intent it = new Intent(this, Teste2Activity.class);
it.putExtra("cliente", cliente);
startActivity(it);
To read the data, it's very simple:
Cliente c = getIntent().getExtras().getParcelable("cliente");
Now, we'll use Serializable:
Let's use an example similar to what we use in Parcelable:
import java.io.Serializable;
public class Pessoa implements Serializable{
private int codigo;
private String nome;
public static final long serialVersionUID = 100L;
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
By doing so, you can pass objects from the Person class to other activities, even adding them to lists.
See:
ArrayList<Pessoa> pessoas = new ArrayList<Pessoa>();
pessoas.add(new Pessoa(1, "Glauber"));
pessoas.add(new Pessoa(2, "Nelson"));
Intent it = new Intent(this, Tela2Activity.class);
// Caso queira passar a lista toda use
it.putExtra("pessoas", pessoas);
// Caso queira passar apenas um objeto
it.putExtra("pessoa", pessoas.get(0));
startActivity(it);
And now, let's recover what we've been through:
Pessoa pessoa = (Pessoa) getIntent().getSerializableExtra("pessoa");
ArrayList<Pessoa> pessoas = (ArrayList<Pessoa>) getIntent().getSerializableExtra("pessoas");
System.out.println("Pessoa: "+ pessoa.getNome());
System.out.println("Pessoas: "+ pessoas.get(0).getNome());
3. What are the differences in performance between the two implementations?
Parcelable is more efficient than Serializable at runtime.
Here are the test results:
Methodology used in testing:
- Imitate the process of passing an object to an activity by placing an object in a package and calling Bundle # writeToParcel (Parcel, int) and then fetching it back;
- Run this in a loop 1000 times;
- The tested objects are
SerializableDeveloper
and ParcelableDeveloper
(see test reference link)
- Devices tested:
LG Nexus 4 / Android 4.2.2;
Samsung Nexus 10 / Android 4.2.2;
HTC Desire Z / 2.3.3.
Results:
Nexus10Serializable:1,0004ms-Parcelable:0.0850ms-Parcelable10.16xbetter.
Nexus4Serializable:1.8539ms-Parcelable:0.1824ms-Parcelable11.80xbetter.
DesireZSerializable:5.1224ms-Parcelable:0.2938ms-Parcelable17.36xbetter.
Conclusion:
- Parcelableisfaster
- Parcelabletakesextratimetoimplement
- Serializableiseasiertodeploy
References:
Parcelable and Serializable Implementation
Efficiency Test
Official Documentation - Parcelable
Official Documentation - Serializable
Interesting post on StackOverFlow -
What is the purpose of the Serializable interface?