Tuesday, February 5, 2013

Android passing an Object between two activities

In this blog we’ll see how we can pass user defined data from one activity to other. Let us say we have a user defined class Employee having two attributes id and name. In order to enable us to pass object of Employee from 1 activity to other the Student class needs to implement the interface called Parcelable. This interface is for classes whose instances can be written to and restored from a Parcel.

Make a project Named AndroidPassingObject and have a main activity with the name ActivityOne .java

ActivityOne.java


package com.jitesh.androidpassingobject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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

public class ActivityOne extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
                Bundle b = new Bundle();
                Student e = new Student(1, "Jitesh Upadhyay");
                b.putParcelable("parse", e);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
    }
}


Student .java

package com.jitesh.androidpassingobject;

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

public class Student implements Parcelable {
int id;
String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}

 
    public int describeContents() {
        // TODO Auto-generated method stub
        return 1;
    }
 
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeInt(id);
        dest.writeString(name);
 
    }
 
    public Student(Parcel source) {
        // TODO Auto-generated method stub
        id = source.readInt();
        name = source.readString();
 
    }
 
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
 
      
        public Student createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new Student(source);
        }
 
        public Student[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Student[size];
        }
    };
 
}

ActivityTwo .java

package com.jitesh.androidpassingobject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class ActivityTwo extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        TextView textView = (TextView) findViewById(R.id.txt);
        Intent intent = getIntent();
        Bundle b = intent.getExtras();
        Student e = b.getParcelable("parse");
 
        textView.setText(e.getId() + ":" + e.getName());
    }
 
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pass Data"
    android:id="@+id/btn"
    />
</LinearLayout>


activity2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
   
   android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/txt"
   />
     
</LinearLayout>


androidmanifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jitesh.androidpassingobject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ActivityOne"
            android:label="@string/title_activity_activity_one" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="ActivityTwo"></activity>
        
    </application>

</manifest>

Download the source code from the link AndroidPassinObject

No comments:

Post a Comment