applications – Is it possible to fully restrict/disable apps when they are closed?

I would love to have more control over when apps can run on my Android: I wish that apps can’t run in background, unless I whitelist them, or unless I am using them.

“Background Restriction” offers something similar. But it kills the restricted apps when they’re placed in background: I can’t simply leave them open, running in the background, until I close them.

For example, I want my music player to be fully dead when I’m not using it: I don’t want it to run processes in the background. But when I’m using it, it should stay alive until I close it, even if its activity is not in the foreground.
This experience is very natural on desktop computers: most programs work while you leave their window open (background or foreground doesn’t matter) and die as soon as you close it. I wish to have it on Android too, but Background Restriction doesn’t quite offer it.

The “Recents Screen” seems like the perfect tool for what I need: only the apps in the Recents Screen (and those whitelisted) should be allowed to run. As soon as I remove one from the Recents Screen, it should get killed and become fully restricted.

  • Can I obtain my desired behavior by tweaking some configuration or by installing some software?

  • Otherwise, would it be possible to implement it myself? I think it’d be enough to have a tiny script listening to/polling the Recents Screen and toggling the Background Restriction setting for apps as they are added to/removed from the Recents Screen. Are there any APIs to do this available from e.g. bash or python, without needing to create a full fledged app?

I’m using a rooted phone, but a rootless solution would be better of course.