การนำทาง (Navigation) ไปยัง Activity ใหม่ด้วย Intent เมื่อกด Button
ตัวอย่างนี้แสดงวิธีใช้ Intent เพื่อเริ่ม Activity ใหม่ (NewActivity) เมื่อผู้ใช้กดปุ่มใน MainActivity
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด → Finish
ขั้นตอนที่ 2: Layout หลัก - res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="เปิด Activity ใหม่!"
android:textSize="32dp"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java
package app.com.sample;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(v -> openNewActivity());
}
public void openNewActivity() {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
}
ขั้นตอนที่ 4: Layout Activity ใหม่ - res/layout/activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="ยินดีต้อนรับสู่ Activity ใหม่"
android:textColor="@color/colorPrimaryDark"
android:textSize="32dp" />
</RelativeLayout>
ขั้นตอนที่ 5: NewActivity.java
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
}
ขั้นตอนที่ 6: 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=".NewActivity" />
<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>
ประเภท Intent
| ประเภท | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Explicit Intent | ระบุ Class เป้าหมายชัดเจน | new Intent(this, NewActivity.class) |
| Implicit Intent | ระบุ Action ให้ระบบหา App ที่รองรับ | new Intent(Intent.ACTION_VIEW, uri) |