How To Detect Android App Foreground Status
The takeaway from this article
In this article, we’re going to learn how to detect if the app is in the foreground or background. Along with that, we’ll also learn how to detect the visible activity to the user.
Use-cases
In the old days, detecting whether the app is in the foreground or not was one of the common tasks that any android developer used to perform. Back then we didn’t have any fancy libraries like LiveData. So developers were responsible to perform actions when the app was in the foreground.
With the introduction of jetpack libraries, the quality of android development has been improved rapidly. We no longer need to bother if the app is in the foreground or not, because under the hood LiveData will take care of it.
Still with all these fancy libraries, at the core of few common use-cases, we need to write the logic to detect if the app is in the foreground or not. One of the best examples is the chatting application. When you receive a notification regarding a conversation you need to deal with it in two ways based on app foreground status:
- If the app is in the background you can display a notification to the user.
- If the app is in the foreground - then again there are two…