รีสตาร์ท Activity ให้เริ่มต้นใหม่
ตัวอย่างนี้สาธิตวิธีรีสตาร์ท (Restart) Activity ปัจจุบันใน Android โดยใช้ finish() และ startActivity() ด้วย Intent เดิมใน Kotlin
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: เลย์เอาต์ (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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerInParent="true"
android:layout_marginTop="16sp"
android:text="Restart Activity"
android:textColor="@android:color/background_dark" />
</RelativeLayout>
ขั้นตอนที่ 3: โค้ดรีสตาร์ท (src/MainActivity.kt)
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.Random
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var button: Button
private val random = Random()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
// แสดงเลขสุ่มเพื่อดูว่ากิจกรรมรีสตาร์ทจริง
textView.text = "Random Number: ${random.nextInt(100)}"
button.setOnClickListener {
val intent = intent
finish()
startActivity(intent)
}
}
}
ขั้นตอนที่ 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.q11">
<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>
วิธีการอื่นๆ
recreate()- API 11+ รีสตาร์ท Activity โดยอัตโนมัติ (เก็บ State ไว้บางส่วน)Intent(this, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK }- ล้าง Back Stack- ใช้
ProcessPhoenix.triggerRebirth(context)จาก Library สำหรับรีสตาร์ททั้งแอป
วิธี finish() + startActivity(intent) เรียบง่ายที่สุดและทำงานได้ทุก API Level