Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Created July 5, 2018 13:30
Show Gist options
  • Save gbzarelli/94d8f9ca5a9278cd38f233d8580e1433 to your computer and use it in GitHub Desktop.
Save gbzarelli/94d8f9ca5a9278cd38f233d8580e1433 to your computer and use it in GitHub Desktop.
Como detectar se a aplicação está minimizada ou não (How to detect Android application in background or foreground events)
/**
Doc: https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner
belongs to Maven artifact "android.arch.lifecycle:extensions:1.1.1" (include in your dependences)
*/
class MyApplication : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackgrounded() {
Log.d(LOG, "App in background")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onAppForegrounded() {
Log.d(LOG, "App in foreground")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment