Bài viết này trình bày cùng một kỹ thuật với bài trước: sử dụng SharedPreferences để truyền một chuỗi đơn giản từ Activity này sang Activity khác. Đây là cách phù hợp cho dữ liệu nhỏ, cần tồn tại lâu dài.
Các bước thực hiện
Bước 1: Tạo dự án mới
Tạo dự án Android Studio, package com.example.myapplication.
Bước 2: Giao diện nhập liệu (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 để chuyể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="Tiếp theo" />
</LinearLayout>
Bước 3: Lưu và chuyển Activity (src/MainActivity.java)
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private static final String PREFS_NAME = "myKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
String value = editText.getText().toString().trim();
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
prefs.edit().putString("value", value).apply();
startActivity(new Intent(MainActivity.this, SecondActivity.class));
});
}
}
Bước 4: Giao diện hiển thị (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" />
</LinearLayout>
Bước 5: Đọc và hiển thị (src/SecondActivity.java)
package com.example.myapplication;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private static final String PREFS_NAME = "myKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.text_view);
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String value = prefs.getString("value", "");
textView.setText(value);
}
}
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>
Khi nào nên dùng SharedPreferences thay vì Intent/Bundle?
| Tiêu chí | Intent/Bundle | SharedPreferences |
|---|---|---|
| Độ bền | Chỉ trong luồng Intent hiện tại | Bền vững (lưu file XML) |
| Kích thước | Giới hạn ~1MB/Intent | Không giới hạn thực tế (nhưng nên < 100KB) |
| Phạm vi | Chỉ Activity nhận Intent | Toàn app, mọi component truy cập được |
| Hiệu năng | Rất nhanh (truyền bộ nhớ) | Chậm hơn (I/O file) |
| Phù hợp | Dữ liệu luồng công việc, đối tượng phức tạp | Cài đặt, token, trạng thái đăng nhập, tùy chọn người dùng |
Tóm lại: Dùng Intent/Bundle cho truyền dữ liệu luồng nghiệp vụ (workflow). Dùng SharedPreferences cho cấu hình, trạng thái toàn cục, dữ liệu cần giữ sau khi tắt app.