ภาพรวม
ตัวอย่างนี้สาธิตวิธีการ Restart Activity (เริ่มต้น Activity ใหม่) โดยการเรียก finish() แล้วตามด้วย startActivity(getIntent()) ซึ่งมีประโยชน์เมื่อต้องการรีเฟรช UI หรือรีเซ็ตสถานะ
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:textStyle="bold"/>
<Button
android:id="@+id/button"
android:layout_below="@id/textView"
android:layout_marginTop="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Restart Activity"/>
</RelativeLayout>
ขั้นตอนที่ 3: เขียนโค้ด MainActivity (src/MainActivity.java)
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
// แสดงตัวเลขสุ่มเมื่อสร้าง Activity
textView.setText("Random Number: " + random.nextInt(100));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 🔑 วิธี Restart Activity
Intent intent = getIntent();
finish(); // ปิด Activity ปัจจุบัน
startActivity(intent); // เริ่ม Activity ใหม่ด้วย Intent เดิม
}
});
}
}
ขั้นตอนที่ 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>
วิธี Restart Activity อื่นๆ
1. ใช้ recreate() (API 11+) - ง่ายที่สุด
button.setOnClickListener(v -> recreate());
เมธอดนี้จะทำ finish() และ startActivity() ให้อัตโนมัติ รักษา Instance State ไว้
2. ใช้ Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent);
3. Restart ทั้งแอป (Process Death)
// ใช้ PackageManager เพื่อ restart process Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); System.exit(0); // หรือ Process.killProcess(Process.myPid());
ข้อควรระวัง
- State Loss: ข้อมูลใน View/Model จะหาย ถ้าไม่ Save/Restore ใน onSaveInstanceState
- Animation: จะมี Animation การเปลี่ยน Activity (overridePendingTransition เพื่อปิดได้)
- Back Stack: Activity เก่าจะถูกลบออกจาก Back Stack
Best Practice: ใช้ ViewModel + LiveData
แทนที่จะ Restart Activity ให้ใช้ Architecture Components:
// ViewModel คงข้อมูลไว้ได้แม้ Activity Recreate MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class); // ข้อมูลไม่หายเมื่อ Rotate/Restart
การทดสอบ
รันแอป จะแสดงตัวเลขสุ่ม กด "Restart Activity" จะรีโหลดและแสดงตัวเลขสุ่มใหม่
