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

Post a Comment

Popular posts from this blog

Flutter Theme Creation, Programmatic Way

Flutter and Dart is an excellent combination for creating the mobile app with material design with nice GUI. Its lots of widget which help the developer for creating the app UI very fast with respect to the other language. You may face some problem in starting due to comm, curly brace and semicolon but after some time you will find the way how to work with these all stuff and it becomes very easy for you. But in this article, we are not going to discuss all these. In this article, we will handle a very interesting topic on how to create a custom theme or multiple themes and let's user decide which theme to use.



Create a flutter project



So let's create a flutter project by selecting the appropriate option from the File menu in android studio or Visual Code. If you are new to Flutter and Dart you can check out our recent post to get started Creating Cross-platform app using flutter.



Once your project is created you will get the default counter app and you try running the app in the…

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 sync completed.
Update the project build config …

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
There is s…