Bundle là cách chuẩn và được khuyến nghị để đóng gói dữ liệu khi chuyển qua lại giữa các Activity thông qua Intent. Nó hỗ trợ nhiều kiểu dữ liệu cơ bản và đối tượng thực hiện Parcelable/Serializable.
Ví dụ hoàn chỉnh
Bước 1: Tạo project mới
Android Studio → File → New Project → Empty Activity.
Bước 2: Giao diện MainActivity (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Nhập nội dung cần truyền"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="Gửi qua Bundle" />
</LinearLayout>
Bước 3: MainActivity.java - Đóng gói dữ liệu vào Bundle
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = editText.getText().toString().trim();
// Tạo Bundle và put dữ liệu
Bundle bundle = new Bundle();
bundle.putString("key_message", value);
// Có thể put thêm nhiều kiểu khác:
// bundle.putInt("key_age", 25);
// bundle.putBoolean("key_active", true);
// bundle.putParcelable("key_object", myParcelableObject);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle); // hoặc intent.putExtra("bundle_key", bundle);
startActivity(intent);
}
});
}
}
Bước 4: Giao diện SecondActivity (res/layout/activity_second.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".SecondActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp" />
</LinearLayout>
Bước 5: SecondActivity.java - Giải nén Bundle
package com.example.myapplication;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.text_view);
// Lấy Bundle từ Intent
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String value = bundle.getString("key_message", "Không có dữ liệu");
textView.setText("Nhận được: " + value);
} else {
textView.setText("Không nhận được Bundle");
}
}
}
Bước 6: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>
</manifest>
Các phương thức put/get phổ biến của Bundle
| Kiểu dữ liệu | Put | Get |
|---|---|---|
| String | putString(key, value) | getString(key, defaultValue) |
| int | putInt(key, value) | getInt(key, defaultValue) |
| boolean | putBoolean(key, value) | getBoolean(key, defaultValue) |
| double | putDouble(key, value) | getDouble(key, defaultValue) |
| Parcelable | putParcelable(key, value) | getParcelable(key) |
| Serializable | putSerializable(key, value) | getSerializable(key) |
| ArrayList<String> | putStringArrayList(key, list) | getStringArrayList(key) |
Lưu ý quan trọng
- Giới hạn kích thước: Bundle qua Intent bị giới hạn ~1MB (binder transaction buffer). Không truyền bitmap/file lớn trực tiếp.
- Key duy nhất: Đảm bảo key put và get trùng khớp chính xác (case-sensitive).
- Null safety: Luôn kiểm tra
bundle != nulltrước khi get. - Parcelable > Serializable: Hiệu năng cao hơn nhiều, nên dùng cho object tùy chỉnh.
Chạy thử
Run app ▶️, nhập text, bấm nút. SecondActivity sẽ hiển thị nội dung vừa truyền qua Bundle.