How to pass an Object from One Activity to another in Android.

By | November 5, 2012

Hello all

This is a simple post that shows How to pass an Object from One Activity to another in Android.

I am going to pass the following object from One Activity to another.

MyObject{
String name;
String website;
}

This is the class that defines the Object.

[java]
package com.coderzheaven.passarraylist;

import android.os.Parcel;
import android.os.Parcelable;

public class MyObject implements Parcelable{

String name,website;

public MyObject(){

}

private MyObject(Parcel in){
this.name = in.readString();
this.website = in.readString();
}

public String getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website = website;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.website);
}

public static final MyObject.Creator<MyObject> CREATOR = new MyObject.Creator<MyObject>() {
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}

public MyObject[] newArray(int size) {
return new MyObject[size];
}
};
}
[/java]

This is the first activity that sends the object to second activity.

[java]
package com.coderzheaven.passarraylist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.setTitle("Main Activity");

final MyObject myobj = new MyObject();
myobj.setName("Coderz");
myobj.setWebsite("CoderzHeaven.com");

TextView name = (TextView)findViewById(R.id.name);
name.setText("Name : " + myobj.getName());

TextView website = (TextView)findViewById(R.id.website);
website.setText("Website : " + myobj.getWebsite());

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("myobject", myobj);
startActivity(intent);
}
});

}
}
[/java]

This is the second Activity.

[java]
package com.coderzheaven.passarraylist;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
this.setTitle("Second Activity");

Bundle b = getIntent().getExtras();

if(b!=null){
MyObject myobj = (MyObject)getIntent().getExtras().getParcelable("myobject");

TextView name = (TextView)findViewById(R.id.name);
name.setText("Name : " + myobj.getName());

TextView website = (TextView)findViewById(R.id.website);
website.setText("Website : " + myobj.getWebsite());
}
}
}
[/java]

activity_main.xml.

[xml]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold" />

<TextView
android:id="@+id/website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"
android:textStyle="bold" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/website"
android:text="Pass Object to Next Activity" />

</RelativeLayout>
[/xml]

second_activity.xml

[xml]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold" />

<TextView
android:id="@+id/website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"
android:textStyle="bold" />

</RelativeLayout>
[/xml]

PassObject

PassObject

You can download the tutorial PDF from here.
PLease leave your comments.

One thought on “How to pass an Object from One Activity to another in Android.

  1. Pingback: How to pass an object back from a finishing activity to the previous activity in android? | CoderzHeaven

Leave a Reply

Your email address will not be published. Required fields are marked *