'How to look for current running app in Android

I am trying to build an android app in kotlin that can run in background and look for currently running app. For example, let's say i opened YouTube in my phone and then app should automatically turn on night mode and when i close YouTube, app should turn off night mode. Most off the answers here are in Java and outdated/depreciated like GET_TASKS permission.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source