pass person array from one activity to another through bundle
pass person array from one activity to another through bundle
Let’s explain how to pass a person array from one activity to another through a Bundle in Java.
In Android development, it is often necessary to pass data between different components of an application. One of the most common scenarios is passing data from one activity to another. In Java, the Bundle class is used to pass data between activities. A Bundle is essentially a key-value pair container that holds the data to be passed. In this tutorial, we will learn how to pass an array of Person objects from one activity to another using a Bundle.
Step 1: Create a new Android project:
To begin, launch Android Studio and create a new project by selecting “Start a new Android Studio project”. Follow the prompts to create a new project with a name of your choice.
Step 2: Create the Person class:
Next, we need to create a Person class that defines the attributes of a person object. Open the project and create a new Java class by right-clicking on the app folder, selecting “New”, and then selecting “Java Class”. Name the class “Person” and paste the following code:
public class Person { private String name; private int age; private String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public int getAge() { return age; } public String getGender() { return gender; } }
In this code, we have defined a Person class with three attributes: name, age, and gender. We have also created a constructor to set the values of these attributes when a new Person object is created. Additionally, we have created getter methods for each attribute.
Step 3: Create the MainActivity:
Let’s first design the layout of our MainActivlty XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:layout_marginLeft="21dp" android:layout_marginBottom="11dp" android:id="@+id/btnNext"/> </LinearLayout>
Next, we need to create the MainActivity that will contain the code to pass the array of Person objects to another activity. Open the project and open the MainActivity.java file. Add the following code:
package com.panrum.passingbundle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.btnNext); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); // Create an array of Person objects Person[] persons = new Person[] { new Person("John", 25, "Male"), new Person("Jane", 23, "Female"), new Person("Bob", 30, "Male") }; // Create a Bundle to hold the array of Person objects Bundle bundle = new Bundle(); bundle.putSerializable("persons", persons); // Add the Bundle to the intent.putExtras(bundle); startActivity(intent); } }); } }
In this code, we have defined the MainActivity class and added a button that will launch the SecondActivity. When the button is clicked, we create an array of Person objects and add them to a Bundle. We then add the Bundle to the intent and start the SecondActivity.
Step 4: Create the SecondActivity:
Next, we need to create the SecondActivity that will receive the array of Person objects. Open the project and create a new Java class by right-clicking on the app folder, selecting “New”, and then selecting “Java Class”. Name the class “SecondActivity”.
First design the layout of our SecondActivity XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context=".SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:id="@+id/stdName" android:textStyle="bold" android:textSize="32sp"/> </LinearLayout>
Here’s the complete code for the SecondActivity class:
package com.panrum.passingbundle; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); textView = findViewById(R.id.stdName); // Get the Bundle that was passed from the MainActivity Bundle bundle = getIntent().getExtras(); if (bundle != null) { // Get the array of Person objects from the Person[] persons = (Person[]) bundle.getSerializable("persons"); // Display the array of Person objects in the TextView StringBuilder sb = new StringBuilder(); for (Person person : persons) { sb.append(person.getName()).append(", ").append(person.getAge()) .append(", ").append(person.getGender()).append("\n"); } textView.setText(sb.toString()); } } }
In this code, we have defined the SecondActivity class and added a TextView to display the array of Person objects. We get the Bundle that was passed from the MainActivity and retrieve the array of Person objects. We then display the array of Person objects in the TextView.
Conclusion::
In this tutorial, we have learned how to pass an array of Person objects from one activity to another using a Bundle in Java. We created a Person class to define the attributes of a Person object and created the MainActivity to pass the array of Person objects to the SecondActivity. We also created the SecondActivity to receive the array of Person objects and display them in a TextView. Finally, we created the layout files for both activities. I hope this tutorial was helpful in understanding how to pass data between activities in an Android application.