Detecting when your Android app goes background / becomes foreground,
is not so simple task as you might think...
I found this utility git for detecting and notifying this:https://github.com/steveliles/
Please note, that it won't work correctly for all case, but it's still very useful.
For example a share intent, if the share dialog is open and your app is still visible in the background, it won't work.
But after selecting an app to share with, it will.
Anyway, It was perfect for what I needed.