Categories
Android

Passing an object from an Activity to another

The method startActivity(Intent intent) of the class Activity allows you to call a second activity specified using the argument Intent.
You can associate primitive data or primitive data array to the argument Intent and then the second Activity can access them, you can also pass objects of type String using methods of the class Bundle like put*().
In this post I show an example to pass a more complex object between an Activity and another.

Suppose to pass an object Contact like this:

public class Contact {

    private String firstname, lastname;

    public Contact() {
    }

    public String getFirstname() {
	return firstname;
    }

    public String getLastname() {
	return lastname;
    }

    public void setFirstname(String firstname) {
	this.firstname = firstname;
    }

    public void setLastname(String lastname) {
	this.lastname = lastname;
    }
}

The first step is to implement the Parcelable interface

...
public class Contact implements Parcelable {
...

that requires

  • a field CREATOR, public, static and that implements Parcelable.Creator, and I the following is a standard implementation for the Contact class
         public static final Parcelable.Creator<Contact> CREATOR
                 = new Parcelable.Creator<Contact>() {
             public Contact createFromParcel(Parcel in) {
                 return new Contact(in);
             }
    
             public Contact[] newArray(int size) {
                 return new Contact[size];
             }
         };
    

    where YourClass is Contact in my example

  • the previous step requires the presence of a constructor
        private Contact(Parcel in) {
    	readFromParcel(in);
        }
        
        public void readFromParcel(Parcel in) {
    	firstname = in.readString();
    	lastname = in.readString();
        }
    
  • the method writeToParcel, to save the fields firstname and lastname
         public void writeToParcel(Parcel out, int flags) {
    	out.writeString(firstname);
    	out.writeString(lastname);
         }
    

    it is important that the data read in the method readFromParcel are of the same type and in the same order of those written in the method writeToParcel

  • the method describeContents, and its usual implementation is:
         public int describeContents() {
             return 0;
         }
    

    I don’t understand the function of this method, and I don’t know when it should return a value other than 0

Now the class Contact is ready to be passed from one activity to another, the code to insert into the calling activity is

	    Intent intent = new Intent(this, SecondActivity.class);
	    Bundle extras = new Bundle();
	    extras.putParcelable("object_key", contact);
	    intent.putExtras(extras);
	    startActivity(intent);

where SecondActivity is the called activity, that receives and handles the object Contact with the code:

	Intent intent = getIntent();
	Bundle extras = intent.getExtras();
	if (extras != null) {
	    contact = (Contact) extras.getParcelable("object_key");
        }

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.