Created
June 7, 2019 06:13
-
-
Save MikeFot/b383354a6479dd602ffa83f601010cdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppOnVisibilityChangeListener() : LifecycleObserver { | |
var isVisible = MutableLiveData<Boolean>() | |
init { | |
AppLog.d("Initialising") | |
ProcessLifecycleOwner.get().lifecycle.addObserver(this) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) | |
fun onAppBackgrounded() { | |
AppLog.d("App in background") | |
onHidden() | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_START) | |
fun onAppForegrounded() { | |
AppLog.d("App in foreground") | |
onVisible() | |
} | |
private fun onHidden() { | |
isVisible.postValue(false) | |
} | |
private fun onVisible() { | |
isVisible.postValue(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
In Application On Create (and any other observers):
onVisibilityChangeListener.isVisible.observeForever { // do something on changed }