앱 포그라운드, 백그라운드 전환 감지

@HiltAndroidApp
class App : Application(), LifecycleEventObserver {

    var isForeground = false
    private val lifecycle by lazy { ProcessLifecycleOwner.get().lifecycle }

    override fun onCreate() {
        super.onCreate()
        lifecycle.addObserver(this)
    }

    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when (event) {
            Lifecycle.Event.ON_STOP -> {
                isForeground = false
                Log.d(TAG, "앱이 백그라운드로 전환")
            }

            Lifecycle.Event.ON_START -> {
                isForeground = true
                Log.d(TAG, "앱이 포그라운드로 전환")
            }

            else -> {}
        }
    }
}