Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an object from one activity to another on Android

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity.

The code for the customer class:

public class Customer {      private String firstName, lastName, Address;     int Age;      public Customer(String fname, String lname, int age, String address) {          firstName = fname;         lastName = lname;         Age = age;         Address = address;     }      public String printValues() {          String data = null;          data = "First Name :" + firstName + " Last Name :" + lastName         + " Age : " + Age + " Address : " + Address;          return data;     } } 

I want to send its object from one Activity to another and then display the data on the other Activity.

How can I achieve that?

like image 597
Adil Bhatty Avatar asked Apr 29 '10 10:04

Adil Bhatty


People also ask

How do I pass model data from one activity to another in Android?

putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc. getStringExtra() method is for getting the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()


2 Answers

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.

Pseudocode:

//To pass: intent.putExtra("MyClass", obj);  // To retrieve object in second Activity getIntent().getSerializableExtra("MyClass"); 

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {      public MainClass() {}      public static class ChildClass implements Serializable {          public ChildClass() {}     } } 
like image 136
Samuh Avatar answered Oct 08 '22 18:10

Samuh


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 warnings 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); startActivity(i); 

In Y activity we are getting the object.

Intent i = getIntent(); Deneme dene = (Deneme)i.getSerializableExtra("sampleObject"); 

That's it.

like image 20
Mustafa Güven Avatar answered Oct 08 '22 19:10

Mustafa Güven