How to determine if one of my activities is in the foreground [duplicate]

The following answer: "Is application running in background", summarizes solutions available for background/foreground checking.

Note:
Previously this answer suggested to use ActivityManager.getRunningAppProcesses(), however that method appeared to be not completely reliable and its usage is discouraged. Check the link above for the details.


Your activity can track its own state as to whether it is in the foreground (set boolean to true in onStart(), to false in onStop()). Alas, that boolean is not provided to you by Activity automatically.