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

วิธีใช้ Singleton Dialog ใน Android (รูปแบบพื้นฐาน)

ภาพรวม

ตัวอย่างนี้สาธิตการใช้ Singleton Dialog ใน Android แบบไม่ใช้ Synchronized เหมาะสำหรับกรณีที่เรียกใช้จาก Main Thread เท่านั้น หรือไม่ต้องการความซับซ้อนของ Thread Safety

ขั้นตอนการสร้าง

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

Android Studio → File → New Project → กรอกรายละเอียด

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <Button
        android:id="@+id/start"
        android:text="เริ่ม Dialog ใน Singleton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/end"
        android:text="ปิด Dialog Singleton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity (src/MainActivity.java)

package com.example.andy.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button start;
    SingleTonExample singletonExample;

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

        start = findViewById(R.id.start);
        singletonExample = SingleTonExample.getInstance();

        findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singletonExample.dismiss();
            }
        });

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singletonExample.show(MainActivity.this);
            }
        });
    }
}

ขั้นตอนที่ 4: สร้างคลาส Singleton (SingleTonExample.java)

package com.example.andy.myapplication;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;

public class SingleTonExample {
    private Dialog dialog;
    private static final SingleTonExample ourInstance = new SingleTonExample();

    public static SingleTonExample getInstance() {
        return ourInstance;
    }

    private SingleTonExample() { }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(true);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

ขั้นตอนที่ 5: สร้าง Layout สำหรับ Dialog (res/layout/layout_progress_dialog.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progress" />

</LinearLayout>

การทดสอบ

รันแอปพลิเคชัน หน้าจอเริ่มต้นแสดงตามภาพ

วิธีใช้ Singleton Dialog ใน Android (รูปแบบพื้นฐาน)

คลิกปุ่ม "เริ่ม Dialog ใน Singleton" จะแสดง Dialog จาก Singleton Class

วิธีใช้ Singleton Dialog ใน Android (รูปแบบพื้นฐาน)

ความแตกต่างจากเวอร์ชัน Synchronized

คุณสมบัติ Basic Singleton Synchronized Singleton
Thread Safety ไม่มี มี (Method Level)
Performance เร็วกว่าเล็กน้อย ช้าลงเล็กน้อยจาก Lock
เหมาะสำหรับ Single-threaded UI Multi-threaded Environment

ข้อควรระวัง

  • การใช้ Dialog โดยตรงใน Singleton อาจทำให้เกิด Memory Leak หาก Context เป็น Activity
  • ควรใช้ Application Context หรือ DialogFragment แทน
  • Dialog จะไม่รอดชีวิตเมื่อ Configuration Change (เช่นหมุนหน้าจอ)