Pass complete object from one activity to another in android Java

Send-Receive class object with data bind inside it to main activity to another using Serializable class using Intent.

Class object is used to access calls function into another class after declaring its that class object. But application developer can send the whole class function with the use of Intent to another class so all the data set into previous class will goto next destination class using Intent. So here is the complete step by step tutorial for Pass complete object from one activity to another in android Java.

android-project-download-code-button

How to Pass complete object from one activity to another in android Java.

Code for MainActivity.java file.

 package com.passobjecttoanotheractivity_android_examples.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button SendObject;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 SendObject = (Button)findViewById(R.id.button1);
 SendObject.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 information info = new information();
 intent = new Intent(getApplicationContext(),SecondActivity.class);
 intent.putExtra("info", info);
 startActivity(intent);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.passobjecttoanotheractivity_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Main Activity"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to send complete object to another activity" />

</RelativeLayout>

Code for SecondActivity.java file.

 package com.passobjecttoanotheractivity_android_examples.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

 TextView name,rollno;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 
 name = (TextView)findViewById(R.id.textView1);
 rollno = (TextView)findViewById(R.id.textView2);
 
 Intent intent = getIntent();
 information Information = (information)intent.getSerializableExtra("info");
 
 name.setText(Information.NAME().toString());
 rollno.setText(String.valueOf(Information.Roll_Number()));
 }
}

Code for activity_second.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.passobjecttoanotheractivity_android_examples.com.SecondActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Name Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="41dp"
 android:text="Roll Number Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Second Activity"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for information.java class file.

 package com.passobjecttoanotheractivity_android_examples.com;

import java.io.Serializable;

public class information implements Serializable{
 
 int roll_no;
 String name;
 
 public int Roll_Number()
 {
 roll_no = 7;
 return roll_no;
 }
 
 public String NAME()
 {
 name = "Android-Examples.com";
 return name;
 
 }
}

Screenshot:

Pass complete object from one activity to another in android Java

Click here to download Pass complete object from one activity to another in android Java project.

Leave a Reply

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