Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีไปหน้า Home Screen ด้วยปุ่มใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีสร้างปุ่มที่เมื่อกดจะพาไปยัง Home Screen (Launcher) ของอุปกรณ์ โดยใช้ Intent ACTION_MAIN + CATEGORY_HOME

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 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"
    android:padding="16sp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Go to Home"/>

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (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;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
            }
        });
    }
}

ขั้นตอนที่ 4: Manifest

<?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>

การทดสอบ

รันแอป กดปุ่ม "Go to Home" - จะกลับสู่ Home Screen ของ Launcher

คำอธิบาย Flag สำคัญ

  • FLAG_ACTIVITY_NEW_TASK - จำเป็นเพราะเริ่ม Activity จาก Context ที่ไม่ใช่ Activity (แต่ที่นี่อยู่ใน Activity ก็ใช้ได้)
  • CATEGORY_HOME - ระบุว่าเป็น Home Activity
  • ACTION_MAIN - Entry point หลัก

กรณีการใช้งานจริง

  • แอป Launcher / Home Screen Replacement
  • แอป Kiosk Mode ที่ต้องการปิดกิจกรรมและกลับหน้าหลัก
  • ปุ่ม "Exit to Home" ในแอปเด็ก/การศึกษา

หมายเหตุ

行為อาจแตกต่างไปตาม Launcher ที่ผู้ใช้ตั้งค่าไว้ (Pixel Launcher, One UI, MIUI, ฯลฯ)