0% found this document useful (0 votes)
8 views10 pages

Android Bundle Usage with Example

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Android Bundle Usage with Example

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bundle in Android with Example

It is known that Intents are used in Android to pass to the data from one
activity to another. But there is one another way, that can be used to pass
the data from one activity to another in a better way and less code space
ie by using Bundles in Android. Android Bundles are generally used for
passing data from one activity to another. Basically here concept of key-
value pair is used where the data that one wants to pass is the value of
the map, which can be later retrieved by using the key. Bundles are used
with intent and values are sent and retrieved in the same fashion, as it is
done in the case of Intent. It depends on the user what type of values the
user wants to pass, but bundles can hold all types of values (int, String,
boolean, char) and pass them to the new activity.

The following are the major types that are passed/retrieved to/from a
Bundle:
putInt(String key, int value), getInt(String key, int value)
putString(String key, String value), getString(String key, String value)
putStringArray(String key, String[] value), getStringArray(String key,
String[] value)
putChar(String key, char value), getChar(String key, char value)
putBoolean(String key, boolean value), getBoolean(String key, boolean
value)
Using the Bundle in the Android App

The bundle is always used with Intent in Android. Now to use Bundle
writes the below code in the MainActivity.
Java

// creating a intent

Intent intent = new Intent(this, [Link]);

// creating a bundle object

Bundle bundle = new Bundle();

// storing the string value in the bundle

// which is mapped to key

[Link]("key1", "GFG :- Main Activity");

// passing the bundle into the intent

[Link](bundle);

// starting the intent

startActivity(intent);

Now create another empty activity named SecondActivity. Now to


retrieve the data stored in the Bundle, write the following code in
SecondActivity.
Java

// getting the bundle back from the android

Bundle bundle = getIntent().getExtras();

// getting the string back

String title = [Link]("key1", "Default");

Alternatively, if one does not want to use the default value too, one can
do this but remember it gives an exception.
For eg: boolean b = [Link](“pass the key here”);
If there exists no mapping corresponding to the key, it may lead to
NullPointerException. Hence it’s recommended to add default values for
the Bundle.

Example:

Step 1: Create a new project


1. Click on File, then New => New Project.
2. Choose Empty activity
3. Select language as Java/Kotlin
4. Select the minimum SDK as per your need.

Step 2: Working with the activity_main.xml file


Now add two Buttons into the app, one button will pass the data which
is stored into the bundle, and another button will pass the empty bundle,
ie clearing the bundle with [Link]() and then passing the Bundle
to Intent. The complete code for the activity_main.xml file is given
below. Here one can see that the first button is used to pass the non-
empty bundle, while the second button is used to pass the empty bundle.
XML

<?xml version="1.0" encoding="utf-8"?>

<[Link]

xmlns:android="[Link] "

xmlns:app="[Link]

xmlns:tools="[Link] "

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="10dp"

tools:context=".MainActivity">

<Button

android:id="@+id/btnPassBundles"

android:layout_width="275dp"

android:layout_height="101dp"

android:layout_marginTop="250dp"

android:text="Pass Data Into Bundle"

android:textSize="24sp"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<Button

android:id="@+id/btnNoPassBundle"

android:layout_width="277dp"

android:layout_height="92dp"

android:layout_marginBottom="220dp"

android:layout_marginTop="75dp"
android:text="Pass No Data/Empty BUNDLE"

android:textSize="24sp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@+id/btnPassBundles" />

</[Link]>

Step 3: Create another activity and named it as SecondActivity

Now create another empty activity names SecondActivity. Follow the


procedure illustrated in the image given below to create another activity.
Step 4: Working with the activity_second.xml file

In this file add a TextView to display the text in the SecondActivity.

XML

<?xml version="1.0" encoding="utf-8"?>

<[Link]

xmlns:android="[Link] "

xmlns:app="[Link]

xmlns:tools="[Link] "

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".SecondActivity">

<TextView

android:id="@+id/txtString"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="348dp"

android:text="String from MainActivity"

android:textSize="40sp"

android:textStyle="bold"

android:gravity="center"

android:textColor="#008000"

app:layout_constraintHorizontal_bias="0.428"

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</[Link]>

Step 5: Working with the MainActivity file

The complete code for the MainActivity is given below. Comments are
added to understand the code easily.

Java

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity implements


[Link] {

Button btnPassBundles, btnNoPassBundle;

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);
setContentView([Link].activity_main);

btnPassBundles = findViewById([Link]);

btnNoPassBundle = findViewById([Link]);

// one button will pass the bundle and other button

// will not pass the bundle

[Link](this);

[Link](this);

@Override

public void onClick(View view) {

switch ([Link]()) {

case [Link]:

// creating a bundle instance

Bundle bundle = new Bundle();

// passing the data into the bundle

[Link](

"key1",

"Passing Bundle From Main Activity to 2nd


Activity");

Intent intent = new Intent([Link],


[Link]);

// passing the bundle to the intent

[Link](bundle);

// starting the activity by passing the intent

// to it.
startActivity(intent);

break;

case [Link]:

bundle = new Bundle();

[Link](

"key1",

"Not passing Bundle From Main Activity");

// clearing the data stored into the bundle

[Link]();

// passing the intent to the second activity

intent = new Intent([Link],


[Link]);

[Link](bundle);

startActivity(intent);

break;

Kotlin

Step 6: Working with the SecondActivity file

The complete code for the SecondActivity is given below. Comments are
added to understand the code easily.
Java

import [Link];

import [Link];

import [Link];

public class SecondActivity extends AppCompatActivity {

TextView txtString;

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_second);

txtString = findViewById([Link]);

// getting the bundle from the intent

Bundle bundle = getIntent().getExtras();

// setting the text in the textview

[Link]([Link]("key1", "No value from the


MainActivity"));

You might also like