The top solution for this post: How to get recent tasks on Android "L"? is about getting which app is running in the foreground.
The method shows how to query all usage stats, and then read the top-most entry to know which app is running in the foreground. But it could only detect single apps. Once the app goes split-screen, it can no longer be detected.
I would like to ask for a solution that could detect all foreground apps running, using real-time queries.
A sidenote: When I try to use the UsageEvents.Event approach to obtain the live states of these packages (denoted by the enums ACTIVITY_PAUSED and ACTIVITY_RESUMED) it did not work for me because the states are completely wrong. I suspect that this is a bug on Android's side. See: https://developer.android.com/reference/android/app/usage/UsageEvents.Event