How to use a Bundle with a non-primitive type?

4

I have the following class created by me:

public class Telefone {

private String nome;
private String telefone;

public String getNome() { 
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getTelefone() {
    return telefone;
}

public void setTelefone(String telefone) {
    this.telefone = telefone;
}

}

And I wanted to pass an instance of this class through the bundle like this:

Telefone telefone = new Telefone();
telefone.setNome("nome");
telefone.setTelefone("12345");
Intent intent = new Intent();
Bundle b = new Bundle();
b.put("telefone", telefone); //como passa esse objeto para o Bundle?
intent.putExtras(b);

I know that Bundle can pass String , int , float that are primitive types, but how to pass an object of type Telefone , as in the example described above?     

asked by anonymous 28.10.2014 / 21:34

2 answers

5

In order for a Bundle to pass non-primitive types, they must implement the Serializable or Parcelable .

The implementation of Serializable is simpler but, in android, can create performance problems.

The implementation of Parcelable might at first glance seem complicated, but only laborious (1) .

Implementation example:

public class DeviceEntity implements Parcelable{

    private long id;
    private String name;
    private String codigo;
    private String number;
    private boolean isSelected;

    public DeviceEntity(long id, String name, String codigo, String number, boolean isSelected){

        this.id = id;
        this.name = name;
        this.codigo = codigo;
        this.number = number;
        this.isSelected = isSelected;
    }

    //Parcelable implementation

    @Override
    public int describeContents() {

        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        // Necessitamos de escrever cada um dos campos no parcel.
        // Quando formos ler do parcel estes são retornados pela mesma ordem
        dest.writeLong(id);
        dest.writeString(name);
        dest.writeString(codigo);
        dest.writeString(number);
        dest.writeByte((byte)(isSelected ? 1 : 0));

    }

    public static final Parcelable.Creator<DeviceEntity> CREATOR = new
            Parcelable.Creator<DeviceEntity>() {

                @Override
                public DeviceEntity createFromParcel(Parcel source) {

                    return new DeviceEntity(source);
                }

                @Override
                public DeviceEntity[] newArray(int size) {

                    throw new UnsupportedOperationException();
                    //return new DeviceEntity[size];
                }
            }
    ;

    //Construtor usado pelo android para criar a nossa classe, neste caso DeviceEntity
    public DeviceEntity(Parcel source) {

        //Os valores são retornados na ordem em que foram escritos em writeToParcel
        id = source.readLong();
        name = source.readString();
        codigo = source.readString();
        number = source.readString();
        isSelected = source.readByte() != 0;
    }

    //Getters

    public long getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getCodigo() {
        return codigo;
    }
    public String getNumber() {
        return number;
    }
    public boolean isSelected() {
        return isSelected;
    }
}

To pass using:

intent.putExtra("DeviceEntity", DeviceEntity);

To receive:

Bundle b = getIntent().getExtras();
DeviceEntity deviceEntity = b.getParcelable("DeviceEntity");

(1) - If you are using Android Studio there is a possibility that it will do the job for you. After adding implements Parcelable , in the class declaration, place the cursor on it and use alt + enter . In the menu that opens choose Add Parcelable Implementation .

    
28.10.2014 / 22:27
3

You can pass directly on Intent , so you need to add an implementation to your Telefone class:

public class Telefone implements Serializable

And then:

intent.putExtra("telefone", telefone);

In Activity will receive as follows:

Intent intent = getIntent();
telefone = (Telefone)intent.getSerializableExtra("telefone");
    
28.10.2014 / 21:59