I'm looking about how to know whether my app is in foreground or background. I've seen two possible solutions, but I'm not sure about which one is better (of if there's a better one).
Solution 1: On my application class create two counter:
private int mResumedActivities = 0;
private int mPausedActivities = 0;
and increment each counter on each onResume/onPause, so I could check if the app is in background as:
public boolean isInBackground() {
return mPausedActivities >= mResumedActivities;
}
Solution 2: Create two booleans in my application class: private boolean isAppResumed = false; private boolean isAppInForeground = false;
and on each onResume/onPause update the isAppResumed and, on each onResume set the isAppInForeground and, on each onPaused trigger a handler with delay so, if after a few milliseconds the app is still paused, then update the isAppInForeground to true.
I think the first solution is simpler, but I don't like having two counters that may be forever incremented. Furthermore, the onPause will be called first, and then the onResume, so there will be some milliseconds that the app may think that this is in foreground when it is really no. What do you think?
Thank you in advance!