Explaining Bundle in Android Development

The bundle type of class inherited from the BaseBundle class is present in the android.os package. By creating an object for the Bundle class, data can be saved in the app. It's a key-pair values data sharing in between the activities of the Android app. Primitive data types like int, string, boolean, and ArrayList can be shared from one activity to another activity. By using putInt() in the First Activity and using getInt() function in the second activity Bundle data are used in the application.

putInt(String key, int value)          ==>   getInt(String key, int value)
putString(String key, String value)    ==>   getString(String key, String value)
putBoolean(String key, boolean value)  ==>   getBoolean(String key, boolean value)
putStringArrayList(String key, ArrayList<String> value) ==> getStringArrayList(String key, ArrayList<String> value)

MainActivity and Activity2 to call are Examples here,

// FirstActivity.java
public void callActivity2(View view) {
    String stEmail = email.getText().toString();
    String stPwd = password.getText().toString(); 
    Intent objIntent = new Intent(MainActivity.this, Activity2.class);
    
    Bundle bundle = new Bundle();
    bundle.putString("email", stEmail);
    bundle.putString("pwd", stPwd);
    objIntent.putExtras(bundle);
    
    startActivity(objIntent);
}
// Activity2.java
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    txt1 = (TextView) findViewById(R.id.textView3);
    txt2 = (TextView) findViewById(R.id.textView4);

    Bundle bundle = getIntent().getExtras();
    String data1 = bundle.getString("email");
    String data2 = bundle.getString("pwd");

    txt1.setText(data1);
    txt2.setText(data2);

    Toast.makeText(getBaseContext(), "Successfully Login Done", Toast.LENGTH_SHORT).show();
}

MainActivity XML

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/toolbar"
    android:orientation="vertical"
    android:background="#11AFAF"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/editTextTextEmailAddress3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editTextNumberPassword2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberPassword" />

    <Button
        android:id="@+id/button2"
        android:layout_width="186dp"
        android:layout_height="wrap_content"
        android:onClick="callActivity2"
        android:text="Login" />

</LinearLayout>

Activity2 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    tools:context=".Activity2"
    android:background="#345F"> 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="291dp"
        android:layout_height="83dp"
        android:text="TextView"
        tools:ignore="MissingConstraints" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="295dp"
        android:layout_height="87dp"
        android:text="TextView"
        android:layout_marginTop="48dp"
        tools:ignore="MissingConstraints" />

</RelativeLayout>

Output

Login

Output


Similar Articles