Skip to main content

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;

public int describeContents() {
return 0;

public void writeToParcel(Parcel parcel, int i) {


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

public Data[] newArray(int i) {
return new Data[i];
public Data(Parcel in){

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.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;
protected void onCreate(Bundle savedInstanceState) {
//Create Data and add to list
dataList= new ArrayList<<Data>>();
Data data= new Data();
// Create Intent and assign the parceable List for sending to second activity on btn click
Button sndBtn= (Button) findViewById(;
sndBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent= new Intent(FirstActivity.this,SecondActivity.class);
intent.putParcelableArrayListExtra("data",(ArrayList) dataList);

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.os.Bundle;
import android.util.Log;

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

public class SecondActivity extends AppCompatActivity {
private List<Data> recList;
protected void onCreate(Bundle savedInstanceState) {
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());


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.


  1. […] If you would like to check the Java version of this tutorial you can check the  Send Array Of Objects To Activity – Android Parcelable […]

  2. I am attracted by the presentation of this article. This information about Parcel, is really good. I really appreciate your work. It is a gainful article for us. Keep posting. Thank you.
    Small Parcel Contract Negotiation


Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

WordPress Android App with REST API And Retrofit

Retrofit is the library which converts your JSON data into Java object. In this tutorial, we will learn about how to implement the Retrofit to create a WordPress Mobile App. As we all know, WordPress is the one of the biggest CMS (Content Management System) of the world. Wordpress full fill the requirements for all most every type of the website. So, in this tutorial, I am going to learn how to create an Android App using the WordPress REST API and Retrofit. What is Retrofit? If you want to know about Retrofit, I would like to give you just a simple one-line explanation of the Retrofit. It is a library which converts your REST HTTP request to Java interface. We will learn how we can do all these stuff using some very simple steps. Further, you can check the Retrofit Library here . So, In this tutorial, we will use the Retrofit to retrieve all post and there except ( A short Description of Post) form a WordPress website. Prerequisite for using the Retrofit for WordPress Android App Ther

How to Read and Write JSON data in Kotlin with GSON

Kotlin is now official language for Android development and it is well supported in Android Studio. So here in this tutorial, we are going to learn about how to read and write JSON data in Kotlin using GSON. If you would like to learn the java version of this tutorial check out the last tutorial " How to Read and Write JSON data using GSON ". Introduction In this tutorial, we will write two methods. In the first method, we will create a JSON file and in second method we will read the file and print in a text box.  If you like to know more about the basic entity of JSON you can check out Basic JSON entity here . What is JSON? JSON stands for J ava S cript O bject N otation JSON is a lightweight data-interchange format It is "self-describing" and easy to understand JSON is language independent and can we used in any language JSON Syntax Rules Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold