Monthly Archives: July 2013

How to carry an Object through an Intent to the Next Activity in Android Using “Parcelable” interface

How to carry an Object through an Intent to the Next Activity in Android Using “Parcelable” interface

Download the Sample Demo of this app here ParcelableDemo.

We all know that we carry data from one activity to another activity using Intents in Android. We put the data into the intent as extra data in the form of key value pair. And the targeted activity will call its method “getIntent()” to get the intent because of which that activity started and then we can get the data in the form of a Bundle object by call getExtras() method on that intent.

 

However we can not directly carry an object through an Intent by putting it as extra data.

 

An Intent can contain objects of only those classes which are implementing “Parcelable” interface or “Serializable” interface.

Here we will be talking about the “Parcelable” interface.

 

In this example we have created a class UserInfo,which will be holding the information about the user i.e. UserName and UserID.

 

We would like to put an Object of UserInfo as extra data and receive into another activity which will be activated by the Intent.
To be able to put UserInfo object into the Intent object, the UserInfo class must implement Parcelable interface.

UserInfo.java

package in.bitcode.parcelabledemo;import android.os.Parcel;
import android.os.Parcelable;

public class UserInfo implements Parcelable {

private String userName;
private int userID;

public static final Parcelable.Creator<UserInfo> CREATOR = new UserInfoCreatorFromParcel();

public UserInfo(String userName, int userID) {
this.userName = userName;
this.userID = userID;
}

public int getUserID() {
return userID;
}

public String getUserName() {
return userName;
}

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

@Overrid
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(userName);
dest.writeInt(userID);
}

private static class UserInfoCreatorFromParcel implements Parcelable.Creator<UserInfo> {

@Override
public UserInfo createFromParcel(Parcel source) {
return new UserInfo( source.readString(), source.readInt() );
}

@Override
public UserInfo[] newArray(int size) {
return null;
}

}

}

 

When an Object of UserInfo is added to intent by using putExtra method of intent, UserInfo objects writeToParcel method is called.

 

Intent intent = new Intent( this, UserAct.class );
intent.putExtra( “userinfo”, new UserInfo( “TestUserName”, 100 ) );

writeToParcel( Parcel dest, int flag ) method will accept following arguments dest of type Parcel and flag of type int.

dest: It is the Parcel to which the data of the UserInfo object must be written.
Flag: It is the int flag indicating how the data must be written to the Parcel.

To make sure that you are able to read back the UserInfo object, the UserInfo class must have a public static member “CREATOR” of type Parcelable.Creator<YourClass>. ( In this case YourClass must be replaced with UserInfo).

Following is the member method of Creator interface

public YourClass createFromParcel(Parcel in) {
//create an object of YourClass  and fill in the data by reading it
//from the Parcel

return new YourClass();
}

 

This method is called when you read the YouClass object back from the Intent.

To get the same object from the intent, you need to call the Intents getParcelableExtra method with the matching key.

UserInfo userInfo = intent.getParcelableExtra(“userinfo”);

Following Demo demonstrates the user of Parcelable

 

Activity_main.xml

 

<LinearLayout 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”

android:orientation=“vertical” >

 

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginLeft=“10dip”

android:layout_marginRight=“10dip”

android:layout_marginTop=“10dip”

android:gravity=“center_vertical” >

 

<ImageView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:src=“@drawable/bitcode” />

 

<TextView

android:id=“@+id/textView1″

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“30dip”

android:text=“Parcelable Demo”

android:textAppearance=“?android:attr/textAppearanceLarge” />

</LinearLayout>

 

<EditText

android:id=“@+id/edtUserID”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:hint=“User ID”

android:inputType=“numberDecimal” >

 

<requestFocus />

</EditText>

 

<EditText

android:id=“@+id/edtUserName”

android:layout_width=“match_parent”

android:layout_height=“wrap_content” />

 

<Button

android:id=“@+id/btnNext”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“Write to Parcel and Start Next Activity” />

 

</LinearLayout>

 

 

MainActivity.Java

 

package in.bitcode.parcelabledemo; 

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

 

public class MainActivity extends Activity {

 

private EditText edtUserName, edtUserID;

private Button btnNext;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

setContentView(R.layout.activity_main);

 

init();

 

btnNext.setOnClickListener(new View.OnClickListener() {

 

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, UserAct.class);

 

String userName = edtUserName.getText().toString();

int userID = Integer.parseInt(edtUserID.getText().toString());

 

intent.putExtra(“userinfo”, new UserInfo(userName, userID));

 

startActivity(intent);

}

});

 

}

 

private void init() {

edtUserName = (EditText) findViewById(R.id.edtUserName);

edtUserID = (EditText) findViewById(R.id.edtUserID);

btnNext = (Button) findViewById(R.id.btnNext);

}

 

}

 

 

 

activity_useract.xml

 

 

<LinearLayout 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”

android:orientation=“vertical” >

 

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginLeft=“10dip”

android:layout_marginRight=“10dip”

android:layout_marginTop=“10dip”

android:gravity=“center_vertical” >

 

<ImageView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:src=“@drawable/bitcode” />

 

<TextView

android:id=“@+id/textView1″

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“30dip”

android:text=“Parcelable Demo”

android:textAppearance=“?android:attr/textAppearanceLarge” />

</LinearLayout>

 

<TextView

android:id=“@+id/txtUserID”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“10dip”

android:textSize=“20sp” />

 

<TextView

android:id=“@+id/txtUserName”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“10dip”

android:textSize=“20sp” />

 

</LinearLayout>

 

 

UserAct.java

 

package in.bitcode.parcelabledemo; 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

 

public class UserAct extends Activity {

 

private TextView txtUserName, txtUserID;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

init();

 

}

 

private void init() {

txtUserName = (TextView) findViewById(R.id.txtUserName);

txtUserID = (TextView) findViewById(R.id.txtUserID);

 

Intent intent = getIntent();

UserInfo userInfo = intent.getParcelableExtra(“userinfo”);

 

txtUserID.setText(“User ID: ” + userInfo.getUserID());

txtUserName.setText(userInfo.getUserName());

}

 

}

 

 

Download the Sample Demo of this app here ParcelableDemo.