How to transfer data between the activities using parcelable in an android

When we have to pass mixed  data types or objects or array of objects from one activity to  another activity then we use parcelable.

When we pass the data from one activity to another activity then we use Intent.

Example:

Inside  First Activity

Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra(“website”,”placement preparation”);
startActivity(intent);

Inside  Second Activity

String name=getIntent.getStringExtra(“website”);

Log.d(“output”, ” “+name);

output=placement preparation

The above technique will work for the integer,float,Boolean etc but not for objects.

But Here we will learn about the Android parcelable step by step:

Suppose you have an array of objects.

List<User> user=[{ “id”:”1”,”name”:”suman”},{“id”:”2”,”name”:”rahul”},........]



Step 1:

First of all,we will create POJO class or setter/getter class according to the objects parameters(id & name).

public class User implements Parcelable
{
private String id;
private String name;

public String getId( )
{
return id;
}
public void setId(String id)
{
this.id=id;
}

public String getName( )
{
return name;
}

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

public User(String id,String name)
{
this.id=id;
this.name=name;
}

protected User(Parcel in)
{
id=in.readId;
name=in.readName;
} 

Public static final Creator<User>=new Creator<User>( )
{
@Override
public User createFromParcel(Parcel in)
{
return new User(in);
}
@Override
Public User[ ] new Array(int size)
{
return new User[size];
}
};

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

public static Creator<User> getCREATOR( )
{
return CREATOR;
}

@Override
public void writeToParcel(Parcel parcel,int i)
{
parcel.writeId(id);
parcel.writeName(name);
}
}

Step 2:

In this step,we will send the data(array of objects) from MainActivity.class or FirstActivity.class to SecondActivity.class.

MainActivity.class

List<User> user=[{ “id”:”1”,”name”:”suman”},{“id”:”2”,”name”:”rahul”},........]

I assume that you have the above list of array.

//Put the below code onClickLister of button or list
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle=new Bundle( );
bundle.putParcelableArraylist(“arrayList”,(ArrayList<? extends Parcelable>) user);
intent.putExtras(bundle);
startActivity(intent);

Step 3:

In this step, we will get  data(ie array of objects) in a SecondActivity.class from the MainActivity.class.

List<User> user=this.getIntent.getExtras( ).getParcelabeArrayList(“arrayList”);

Log.d(“Your_arrayListData=”,” “+user);

Your_arrayListData=
[{ “id”:”1”,”name”:”suman”},{“id”:”2”,”name”:”rahul”},……..]

Hope ,I explained about the parcelable in an android step by step.If you have any query then you can comment me.

Thanks and  Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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