PASSING BUNDLE THROUGH INTENT IN ACTIVITIES

top banner

PASSING BUNDLE THROUGH INTENT IN ACTIVITIES – ANDROID TUTORIAL

When developing Android applications, it is often necessary to pass data between different activities. One way to achieve this is by using a Bundle object. Bundles are used to store and pass data between different components of an application. In this tutorial, we will explore how to use Bundles to pass data between activities in Android.
What is a Bundle? A Bundle is a collection of key-value pairs, where the keys are strings and the values can be any primitive data type, arrays, or other Bundles. Bundles are commonly used in Android to store data and pass it between different components of an application, such as activities, services, and broadcast receivers.
Creating a Bundle: To create a Bundle, we first need to create an instance of the Bundle class. We can then add data to the Bundle using the various put methods provided by the Bundle class. Here is an example of creating a Bundle and adding data to it:
First of all, Let design the MainActivity XML Lay Out:

<?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>

Passing data from one Activity to another Activity in android:

In this example, we will create a Bundle object while Using Intents and Extras to pass data between Activities and will add three key-value pairs to it. The first key is “username”, with a value of “John”. The second key is “age”, with a value of 30. The third key is “isMarried”, with a value of true.
Once we have created a Bundle, we can pass it between activities using an Intent object and using putExtra(). An Intent is a messaging object that can be used to start an activity, service, or broadcast receiver. To pass a Bundle between activities, we need to add the Bundle to the Intent as an extra.
Here is an example of how to pass a Bundle between activities:

Code for Java in the first activity:
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;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnNext;
        btnNext=findViewById(R.id.btnNext);

        Bundle bundle = new Bundle();
        bundle.putString("username", "John");
        bundle.putInt("age", 30);
        bundle.putBoolean("isMarried", true);

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,
                 SecondActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });

    }
}
Retrieving bundle data from android activity:

First of all, we will create a secondActivity in our project for Retrieving bundle data from android activity. If you can create the second activity, then please watch our previous lesson

Now Design the secondactivity XML layout as below:

<?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>

In the second activity, we will retrieve our passed data. So look at the java code to check for Retrieving data from intent in secondActivity as below:

 
package com.panrum.passingbundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView stdName;
        stdName = findViewById(R.id.stdName);

        Bundle bundle = getIntent().getExtras();
        String username = bundle.getString("username");
        int age = bundle.getInt("age");
        boolean isMarried = bundle.getBoolean("isMarried");
        stdName.setText("User Name : " + username + "\n" + "age : " + age
                + "\n" + "isMarried : " + isMarried);

    }
}

In this example, we first create a Bundle object and add some data to it in the first activity. We then create an Intent object and add the Bundle to it as an extra using the putExtras() method. We then start the second activity using the startActivity() method.
In the second activity, we retrieve the Bundle object from the Intent using the getIntent().getExtras() method. We then retrieve the data from the Bundle using the various get methods provided by the Bundle class. In this example, we retrieve the “username” key as a String, the “age” key as an int, and the “isMarried” key as a boolean.

Leave a Reply

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