การทำงานของ onDestroy() ใน Android Activity Lifecycle
ตัวอย่างนี้แสดงวงจรชีวิต (Lifecycle) ของ Activity โดยเฉพาะ onDestroy() ซึ่งถูกเรียกเมื่อ Activity ถูกทำลาย พร้อมบันทึก Log เพื่อดูลำดับการทำงาน
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด → Finish
ขั้นตอนที่ 2: Layout - res/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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:text="Destroy"
android:textColor="@color/colorPrimary"
android:textSize="32dp"
android:textStyle="bold"
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.sample.q2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
public static final String MY_TAG = "Destroy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(MY_TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i(MY_TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(MY_TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(MY_TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(MY_TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(MY_TAG, "onDestroy");
}
}
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
</application>
</manifest>
ลำดับ Activity Lifecycle
- onCreate() - สร้าง Activity ครั้งแรก
- onStart() - Activity มองเห็นได้ (แต่อาจมี Dialog ทับ)
- onResume() - Activity พร้อมโต้ตอบกับผู้ใช้ (Foreground)
- onPause() - เริ่มหายจาก Foreground (ยังมองเห็นได้บางส่วน)
- onStop() - หายจากมุมมองผู้ใช้เลย
- onDestroy() - Activity ถูกทำลายสิ้นสุด
เมื่อไหร่ onDestroy() ถูกเรียก?
- ผู้ใช้กด Back ปิด Activity
- เรียก
finish()ในโค้ด - ระบบต้องการหน่วยความจำ (Low Memory)
- การเปลี่ยนแปลง Configuration (rotate) และไม่ได้ handle เอง
finishAffinity()หรือfinishAndRemoveTask()
⚠️ ข้อควรระวัง
- onDestroy() ไม่รับประกัน จะถูกเรียกเสมอไป (ระบบอาจ kill process ทันที)
- ไม่ควรทำงานสำคัญ (save data, network) ใน onDestroy()
- ใช้ onPause()/onStop() สำหรับบันทึกข้อมูลแทน
- ทำ Cleanup เฉพาะ resource ที่สร้างใน Activity (unregister receiver, close cursor, stop service)
การทดสอบ
รันแอป → ดู Logcat ด้วย Tag "Destroy" → กด Back หรือ Home → สังเกตลำดับ Log