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 , SecondActivity. class ); // creating a bundle object Bundle bundle = new Bundle(); // storing the string value in the bundle // which is mapped to key bundle.putString( "key1" , "GFG :- Main Activity" ); // passing the bundle into the intent intent.putExtras(bundle); // starting the intent startActivity(intent); |
Kotlin
// creating the instance of the bundle val bundle = Bundle() // storing the string value in the bundle // which is mapped to key bundle.putString( "key1" , "Gfg :- Main Activity" ) // creating a intent intent = Intent( this @MainActivity , SecondActivity:: class .java) // passing a bundle to the intent intent.putExtras(bundle) // starting the activity by passing the intent to it. 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 = bundle.getString( "key1" , "Default" ); |
Kotlin
// getting the bundle back from the android val bundle = intent.extras // performing the safety null check var s:String? = null // getting the string back s = bundle!!.getString( "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 = bundle.getBoolean(“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
- Click on File, then New => New Project.
- Choose Empty activity
- Select language as Java/Kotlin
- 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 bundle.clear() 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" ?> < androidx.constraintlayout.widget.ConstraintLayout 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" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
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" ?> < androidx.constraintlayout.widget.ConstraintLayout 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" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
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 android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnPassBundles, btnNoPassBundle; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPassBundles = findViewById(R.id.btnPassBundles); btnNoPassBundle = findViewById(R.id.btnNoPassBundle); // one button will pass the bundle and other button // will not pass the bundle btnPassBundles.setOnClickListener( this ); btnNoPassBundle.setOnClickListener( this ); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnPassBundles: // creating a bundle instance Bundle bundle = new Bundle(); // passing the data into the bundle bundle.putString( "key1" , "Passing Bundle From Main Activity to 2nd Activity" ); Intent intent = new Intent(MainActivity. this , SecondActivity. class ); // passing the bundle to the intent intent.putExtras(bundle); // starting the activity by passing the intent // to it. startActivity(intent); break ; case R.id.btnNoPassBundle: bundle = new Bundle(); bundle.putString( "key1" , "Not passing Bundle From Main Activity" ); // clearing the data stored into the bundle bundle.clear(); // passing the intent to the second activity intent = new Intent(MainActivity. this , SecondActivity. class ); intent.putExtras(bundle); startActivity(intent); break ; } } } |
Kotlin
import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity(), View.OnClickListener { var btnPassBundles: Button? = null var btnNoPassBundle: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) btnPassBundles = findViewById(R.id.btnPassBundles) btnNoPassBundle = findViewById(R.id.btnNoPassBundle) // one button will pass the bundle and other button // will not pass the bundle btnPassBundles?.setOnClickListener( this ) btnNoPassBundle?.setOnClickListener( this ) } override fun onClick(view: View) { when (view.id) { R.id.btnPassBundles -> { // creating the bundle instance val bundle = Bundle() // passing the data into the bundle bundle.putString( "key1" , "Passing Bundle From Main Activity to 2nd Activity" ) val intent = Intent( this @MainActivity , SecondActivity:: class .java) intent.putExtras(bundle) startActivity(intent) } R.id.btnNoPassBundle -> { val bundle = Bundle() bundle.putString( "key1" , "Not passing Bundle From Main Activity" ) // clearing the bundle bundle.clear() // passing the intent to the second activity intent = Intent( this @MainActivity , SecondActivity:: class .java) // passing the bundle into the intent intent.putExtras(bundle) startActivity(intent) } } } } |
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 androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { TextView txtString; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_second); txtString = findViewById(R.id.txtString); // getting the bundle from the intent Bundle bundle = getIntent().getExtras(); // setting the text in the textview txtString.setText(bundle.getString( "key1" , "No value from the MainActivity" )); } } |
Kotlin
import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class SecondActivity : AppCompatActivity() { var txtString: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_second) txtString = findViewById(R.id.txtString) txtBoolean = findViewById(R.id.txtBoolean) // getting the bundle from the intent val bundle = intent.extras // setting the text in the textview txtString?.setText(bundle!!.getString( "key1" , "No value from MainActivity :(" )) } } |
Output: Run on Emulator