Since API 14 you can use the function onTrimMemory()
and check for the flag TRIM_MEMORY_UI_HIDDEN
. This will tell you that your Application is going to the background.
So in your custom Application class you can write something like:
override fun onTrimMemory(level: Int) {
if (level == TRIM_MEMORY_UI_HIDDEN) {
// Application going to background, do something
}
}
For an in-depth study of this, I invite you to read this article: http://www.developerphil.com/no-you-can-not-override-the-home-button-but-you-dont-have-to/