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.

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 CREATOR = new MyObject.Creator() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in);
        }

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

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

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);				
			}
		});
        
    }
}

This is the second Activity.

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());
        }
    }
}

activity_main.xml.



second_activity.xml

PassObject

PassObject

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

Leave a Reply

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