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

วิธีดึง Context ของ Activity ที่อยู่บนหน้าจอ (Foreground) ใน Android

การเข้าถึง Context ของ Activity ที่ทำงานอยู่ใน foreground

ตัวอย่างนี้แสดงวิธีสร้างคลาส Application แบบกำหนดเองเพื่อเก็บอ้างอิงถึง Activity ที่อยู่บนหน้าจออยู่ ณ ขณะนั้น (Foreground Activity) ซึ่งมีประโยชน์เมื่อต้องการเข้าถึง Context จากที่ไหนก็ได้ในแอป

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

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

ขั้นตอนที่ 2: สร้างคลาส MyApp.java (extends Application)

package app.tutorialspoint.com.sample;

import android.app.Activity;
import android.app.Application;

public class MyApp extends Application {
    private Activity mCurrentActivity = null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}

ขั้นตอนที่ 3: สร้าง Base Activity - MyBaseActivity.java

package app.tutorialspoint.com.sample;

import android.app.Activity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MyBaseActivity extends AppCompatActivity {
    protected MyApp mMyApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMyApp = (MyApp) getApplicationContext();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMyApp.setCurrentActivity(this);
    }

    @Override
    protected void onPause() {
        clearReferences();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        clearReferences();
        super.onDestroy();
    }

    private void clearReferences() {
        Activity currActivity = mMyApp.getCurrentActivity();
        if (this.equals(currActivity)) {
            mMyApp.setCurrentActivity(null);
        }
    }
}

ขั้นตอนที่ 4: ใช้งานใน MainActivity.java

package app.tutorialspoint.com.sample;

import android.app.Activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Activity currentActivity = ((MyApp) getApplicationContext()).getCurrentActivity();
        // currentActivity จะเป็น Activity ที่อยู่ foreground อยู่
    }
}

ขั้นตอนที่ 5: ลงทะเบียนใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.sample">
    <application
        android:name=".MyApp"
        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>
        <activity android:name=".MyBaseActivity" />
    </application>
</manifest>

หมายเหตุสำคัญ

  • ทุก Activity ที่ต้องการให้ระบบติดตาม ต้อง extends จาก MyBaseActivity
  • วิธีนี้เหมาะสำหรับแอปที่มี Activity หลายตัวและต้องการเข้าถึง Context ปัจจุบันจากคลาสอื่นๆ
  • ควรระวัง Memory Leak - ต้อง clear reference ใน onPause/onDestroy เสมอ