How To Send Array Of Objects To Activity – Android Parcelable

How To Send Array Of Objects To Activity – Android Parcelable

Creating parcelable object is the solution to send an array of objects to activity. Many times we required to send important data and information from one activity to another activity, we use the Android Intent and bind the integer, long, String etc in the extra parameter of the intent for this purpose.  But, what if we have to send the array of objects to any activity?  So, here we are going to learn about Android Parcelable objects which can be used to send and receive the array of objects to any activity.

Android Parcelable class to Send Array Of Objects To Activity

Parcelable  Class to create and Send Array Of Objects To Activity

In this section, we will create a class which implements parcelable, so that we can use this object class to create and sends an array of objects to Activity as a parcel to the second activity. The class which implements the parcelable interface should also have a non-null static field called ofCREATOR a type that implements the Parceable.Creator interface.

A typical implementation of Parcelable Interface is as given below.


package com.nplix.androidparcelableobject;
 import android.os.Parcel;
 import android.os.Parcelable;/**
 * Created by PK on 4/17/2017.
 */public class Data implements Parcelable{
 private long ID;
 private String FirstName;
 private String LastName;public long getID() {
 return ID;
 }public void setID(long ID) {
 this.ID = ID;
 }

public String getFirstName() {
 return FirstName;
 }

public void setFirstName(String firstName) {
 FirstName = firstName;
 }

public String getLastName() {
 return LastName;
 }

public void setLastName(String lastName) {
 LastName = lastName;
 }

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

@Override
 public void writeToParcel(Parcel parcel, int i) {
 parcel.writeLong(ID);
 parcel.writeString(FirstName);
 parcel.writeString(LastName);

}

public static final Parcelable.Creator CREATOR= new Parcelable.Creator(){
 public Data createFromParcel(Parcel in) {
 return new Data(in);
 }

@Override
 public Data[] newArray(int i) {
 return new Data[i];
 }
 };
 public Data(Parcel in){
 ID=in.readLong();
 FirstName=in.readString();
 LastName=in.readString();
 }
 }

Create Parcelable Array object in First Activity

In our First Activity, we will create the List of the parceable object and pass the object to Second Activity. Below is the complete code of First Activity.

package com.nplix.androidparcelableobject;

 import android.content.Intent;
 import android.os.Parcelable;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;

 import java.util.ArrayList;
 import java.util.List;

public class FirstActivity extends AppCompatActivity {
 private List<<Data>> dataList;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_first);
 //Create Data and add to list
 dataList= new ArrayList<<Data>>();
 Data data= new Data();
 data.setID(1000);
 data.setFirstName("NPLIX");
 data.setLastName(".com");
 dataList.add(data);
 // Create Intent and assign the parceable List for sending to second activity on btn click
 Button sndBtn= (Button) findViewById(R.id.sendButton);
 sndBtn.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Intent intent= new Intent(FirstActivity.this,SecondActivity.class);
 intent.putParcelableArrayListExtra("data",(ArrayList) dataList);
 getApplicationContext().startActivity(intent);
 }
 });
 }
 }

Read the List of Parcelable Object data in Second Activity

For reading the List of Parcelable Object we need to follow below steps.

First, create a list or array as given below.

Declare the List with the type Data


private List<> recList;

Initialize the Array of objects


recList=new ArrayList<>();
recList =(ArrayList) getIntent().getParcelableArrayListExtra(data);

Read the data in Second Activity and print in log. For simplicity of this tutorial we are just printing the information into the logs.

But in the real scenario, you can use these object as per your requirement of the project.

Log.d("NPLIX:- ", "ID:"+ recList.get(0).getID()+"First Name:"+ recList.get(0).getFirstName()+"Last Name:"+ recList.get(0).getLastName());

Second Activity

 package com.nplix.androidparcelableobject;

import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;

import java.util.ArrayList;
 import java.util.List;

public class SecondActivity extends AppCompatActivity {
 private List<Data> recList;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 recList=new ArrayList<Data>();
 recList = (ArrayList) getIntent().getParcelableArrayListExtra("data");

Log.d("NPLIX:- ", "ID:"+ recList.get(0).getID()+"First Name:"+
 recList.get(0).getFirstName()+"Last Name:"+ recList.get(0).getLastName());
 }
 }

Conclusion

So Conclusion of the above short tutorial is that we need to create a POJO class for our object and that should implement the parcelable and implement all method to parcel and read the data. Both should be implemented in the same sequence so that second activity can read the object element in the same sequence as it was sent by first activity.

Get Email Updates!

Signup now receive an email once I publish new content. I will never give away, trade or sell your email address. You can unsubscribe at any time.

Join 911 other subscribers

This Post Has One Comment

Leave a Reply

Close Menu