How can I check my app's state (foreground or background)?

I want to know when my app is foregrounded.
Then I calculate elapsed time and give reward by elapsed time.
But I can’s find a way to check when my app is foregrounded.
Please help me.

I know this is old but apparently nobody found a better way so:
Rama’s famous Victory plugin has a check against this very thing. Just start typing fore ground and you will find it quick. Rama is basically a living saint for the religion of blueprints at this point…