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;
}

@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.

Comments

  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 […]

    ReplyDelete
  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

    ReplyDelete

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

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Scan and List All Available WiFi Network In Android

In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. You May Like Below Topic: How to Read and Write JSON data using GSON WP Android App using REST and volley WP Android App using REST and volley part2 Implementation of SwipeRefreshLayout in RecyclerView Create Amazing Bottom Navigation Bar Without Any External Library Introduction In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. We will add this functionality to our existing Demo app " Video Gallery ". If you would like to check out t