Using the following code you can detect if the App comes foreground.
Read moreWhat is the difference between foreground service and background service?
Background Service is used when even user close application (discard from recents) and when Service is doing something not visible to user like downloading data from server, load data from a ContentProvider etc.. And Foreground Service is less likely to be killed by system on low memory.7 Kas 2011
Read moreWhat does it mean when an app is running in the foreground?
Foreground refers to the active apps which consume data and are currently running on the mobile . Background refers to the data used when the app is doing some activity in the background, which is not active right now.
Read moreWhat is foreground and background services?
A Background Service is a service that runs only when the app is running so it’ll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.1 Eki 2021
Read moreWhat is foreground service permission Android?
Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. The notification cannot be dismissed unless the service is either stopped or removed from the foreground.
Read more