20 Ekim 2011 Perşembe

How to pass a complex object from one activity to another in Android

implement your class with Serializable. Let's suppose that this is your entity class:
import java.io.Serializable;
@SuppressWarnings("serial") //with this annotation we are going to hide compiler warning
public class Deneme implements Serializable {
public Deneme(double id, String name){
    this.id = id;
    this.name = name;
public double getId() {
    return id;
public void setId(double id) {
    this.id = id;
public String getName() {
    return this.name;
public void setName(String name) {
    this.name = name;
private double id;
private String name;
we are sending the object called dene from X activity to Y activity. Somewhere in X activity;
Deneme dene = new Deneme(4,"Mustafa");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", dene);
In Y activity we are getting the object.
Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");
that's it.

Hiç yorum yok:

Yorum Gönder