ตรวจสอบ First Launch ด้วย SharedPreferences
บทความนี้แนะนำวิธีตรวจสอบว่าแอปพลิเคชันถูกเปิดครั้งแรกหรือไม่ โดยใช้ SharedPreferences เพื่อเก็บสถานะการเปิดครั้งแรก วิธีนี้เหมาะสำหรับการแสดง Tutorial, Onboarding Screen หรือข้อความต้อนรับ
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout ไฟล์ activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvFirstTimeStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#212121"
android:padding="24dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ขั้นตอนที่ 3: MainActivity.java - ตรรกะหลัก
package com.example.firstlaunchdemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String PREFS_NAME = "AppPrefs";
private static final String KEY_FIRST_LAUNCH = "isFirstLaunch";
private TextView tvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus = findViewById(R.id.tvFirstTimeStatus);
checkFirstLaunch();
}
private void checkFirstLaunch() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean isFirstLaunch = prefs.getBoolean(KEY_FIRST_LAUNCH, true);
if (isFirstLaunch) {
// ครั้งแรกที่เปิดแอป
tvStatus.setText("🎉 ยินดีต้อนรับ! นี่คือครั้งแรกที่คุณเปิดแอป");
// ทำสิ่งที่ต้องการสำหรับ First Launch เช่น แสดง Tutorial
showOnboarding();
// อัปเดตสถานะว่าเปิดแล้ว
prefs.edit().putBoolean(KEY_FIRST_LAUNCH, false).apply();
} else {
// เปิดซ้ำ
tvStatus.setText("👋 สวัสดีครับ! คุณเคยเปิดแอปนี้มาก่อนแล้ว");
}
}
private void showOnboarding() {
// TODO: เริ่ม Activity/Fragment สำหรับ Onboarding/Tutorial
// startActivity(new Intent(this, OnboardingActivity.class));
}
}
จุดสำคัญที่ต้องทราบ
- getSharedPreferences() vs getPreferences(): ใช้
getSharedPreferences()เพื่อให้เข้าถึงได้จากทุก Activity ในแอปเดียวกัน - apply() vs commit(): ใช้
apply()(asynchronous) แทนcommit()(synchronous) เพื่อประสิทธิภาพดีกว่า - Default value = true: ค่าเริ่มต้นเป็น true เพราะถ้ายังไม่มี key นี้ แสดงว่ายังไม่เคยเปิด
รูปแบบโค้ดที่ปรับปรุง (Best Practice)
public class FirstLaunchManager {
private static final String PREFS_NAME = "AppPrefs";
private static final String KEY_FIRST_LAUNCH = "isFirstLaunch";
private final SharedPreferences prefs;
public FirstLaunchManager(Context context) {
prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
public boolean isFirstLaunch() {
return prefs.getBoolean(KEY_FIRST_LAUNCH, true);
}
public void markAsLaunched() {
prefs.edit().putBoolean(KEY_FIRST_LAUNCH, false).apply();
}
public void reset() { // สำหรับทดสอบ
prefs.edit().putBoolean(KEY_FIRST_LAUNCH, true).apply();
}
}
การใช้งาน:
FirstLaunchManager manager = new FirstLaunchManager(this);
if (manager.isFirstLaunch()) {
showOnboarding();
manager.markAsLaunched();
}
กรณีใช้งานจริง
- แสดงหน้า Onboarding/Tutorial สำหรับผู้ใช้ใหม่
- ตั้งค่า Default preferences
- ส่ง Event Analytics "first_open"
- แสดง Dialog ยินดีต้อนรับหรือข้อความโปรโมชั่น
- Initialize database หรือดาวน์โหลดข้อมูลเริ่มต้น
การทดสอบ
1. รันแอปครั้งแรก → จะแสดงข้อความต้อนรับ
2. ปิดแอป → เปิดใหม่ → จะแสดงข้อความ "เคยเปิดมาก่อนแล้ว"
3. เพื่อทดสอบซ้ำ: Clear App Data ใน Settings หรือ uninstall แล้ว install ใหม่