How to pass data between activities with android Serializable


Using serialization, we can pass object's state or array between two activities. Before getting into the code, we should know about serialization and how does it work with intent in android.

Serialization is a marker interface. Using serialization, we can convert state of an object into a byte stream. The byte stream is a platform independent, so it's going to work on the JVM and other platforms. Here is the example to send an object between two activities.

Step 1 − Create a new project in Android Studio, Go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   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">
<LinearLayout
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:gravity = "center_horizontal"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/name"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:ems = "10"
      android:hint = "Enter a name"
      android:inputType = "text" />
   <EditText
      android:id = "@+id/phone"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:ems = "10"
      android:hint = "Enter a Phone number"
      android:inputType = "number"/>
   <Button
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Send data"
      android:id = "@+id/send"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Step 2 − Following is the content of the modified main activity file MainActivity.java. This file can include each of the fundamental lifecycle methods.

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   EditText name;
   EditText phone;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      name = findViewById(R.id.name);
      phone = findViewById(R.id.phone);
      Button send = findViewById(R.id.send);
      send.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (TextUtils.isEmpty(name.getText().toString()) && TextUtils.isEmpty(phone.getText().toString())) {
               Toast.makeText(MainActivity.this,"Something is wrong kindly check",Toast.LENGTH_LONG).show();
            }else{
sendUserData(name.getText().toString(),phone.getText().toString());
            }
         }
      });
   }
   private void sendUserData(String username, String userPhone) {
      Userinfo userinfo = new Userinfo();
      userinfo.setName(username);
      userinfo.setPhone(userPhone);
      Intent send = new Intent(MainActivity.this,SecondActivity.class);
      Bundle b = new Bundle();
      b.putSerializable("serialzable",userinfo);
      send.putExtras(b);
      startActivity(send);
   }
}

Step 3 − Following is the content of the modified main activity file  SecondActivity.java. This file can include each of the fundamental lifecycle methods. This activity going to access the object from MainActivity as shown below −

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
   Userinfo userinfo;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      TextView data = findViewById(R.id.data);
      userinfo = (Userinfo) getIntent().getSerializableExtra("serialzable");
      String name = userinfo.getName();
      String phone = userinfo.getPhone();
      data.setText("Your entered name is "+name+" number is "+phone);
   }
   @Override
   protected void onPause() {
      super.onPause();
      userinfo = null;
   }
}

Step 3 − Following is the content of the serialiable Userinfo object. This object data we are passing between MainActivity and SecondActivity. To make Userinfo serializable object we should implement the java.io.Serializable interface as shown below −

import java.io.Serializable;
class Userinfo implements Serializable {
   String name;
   String phone;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getPhone() {
      return phone;
   }
   public void setPhone(String phone) {
      this.phone = phone;
   }
}

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen.

 User Name


Password

Now click on Send data button, it will send you to SecondActivity as shown below −

Logged In

In the above output, we got information from MainActivity to SecondActivity

Click here to download the project code

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements