반응형
Application이 Foreground인지, Background 상태인지 ActivityLifecycleCallbacks 를 사용하여 확인해본다.
enum class ApplicationState {
FOREGROUND,
BACKGROUND
}
class ApplicationLifecycleStatus : Application.ActivityLifecycleCallbacks {
companion object {
var state:ApplicationState = ApplicationState.FOREGROUND
}
override fun onActivityStarted(activity: Activity) {
state = ApplicationState.FOREGROUND
}
override fun onActivityStopped(activity: Activity) {
state = ApplicationState.BACKGROUND
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { }
override fun onActivityResumed(activity: Activity) { }
override fun onActivityPaused(activity: Activity) { }
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { }
override fun onActivityDestroyed(activity: Activity) { }
}
ApplicationLifecycleStatus란 클래스는 Application.ActivityLifecycleCallbacks(https://developer.android.com/reference/kotlin/android/app/Application.ActivityLifecycleCallbacks)란 interface를 사용하여 구현을 한다.
구현한 ApplicationLifecycleStatus를 Application()을 상속받은 새로운 클래스에서 Callback으로 등록하여 준다.
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(ApplicationLifecycleStatus())
}
}
반응형
'Android > Component' 카테고리의 다른 글
[Android] 위치(Location) (1) | 2022.07.14 |
---|---|
[Android Wear] Ambient Mode에서 Display 정보 업데이트하기 (0) | 2022.07.10 |
[Android/Unity] Failed to install the following Android SDK packages as some licences have not been accepted. (0) | 2022.05.07 |
[Android/Unity] 터미널 환경에서 빌드하기 (Ubuntu) (2) | 2022.05.05 |
[Android/Unity] TargetSDK version 31 이상에서 Manifest merger failed 문제 (0) | 2022.05.04 |