Skip to content

Commit

Permalink
fix: try to correctly handle session change for race condition (#382)
Browse files Browse the repository at this point in the history
* fix: try to correctly handle session change for race condition

* fix: set isEnteringForeground to false on exit foreground
  • Loading branch information
justin-fiedler authored Jul 21, 2023
1 parent 16d058e commit b0f4fea
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public class AmplitudeClient {
private boolean usingForegroundTracking = false;
private boolean trackingSessionEvents = false;
private boolean inForeground = false;
private boolean isEnteringForeground = false;
private boolean flushEventsOnClose = true;
private String libraryName = Constants.LIBRARY;
private String libraryVersion = Constants.VERSION;
Expand Down Expand Up @@ -1221,7 +1222,8 @@ protected long logEvent(String eventType, JSONObject eventProperties, JSONObject

if (!loggingSessionEvent && !outOfSession) {
// default case + corner case when async logEvent between onPause and onResume
if (!inForeground){
if (!inForeground || isEnteringForeground){
isEnteringForeground = false;
startNewSessionIfNeeded(timestamp);
} else {
refreshSessionTime(timestamp);
Expand Down Expand Up @@ -1573,6 +1575,7 @@ private void sendSessionEvent(final String sessionEvent) {
* @param timestamp the timestamp
*/
void onExitForeground(final long timestamp) {
isEnteringForeground = false;
inForeground = false;
runOnLogThread(new Runnable() {
@Override
Expand Down Expand Up @@ -1602,6 +1605,7 @@ public void run() {
* @param timestamp the timestamp
*/
void onEnterForeground(final long timestamp) {
isEnteringForeground = true;
inForeground = true;
runOnLogThread(new Runnable() {
@Override
Expand All @@ -1617,7 +1621,13 @@ public void onFinished() {
}
}, serverZone);
}
startNewSessionIfNeeded(timestamp);
// This should be true, unless somehow an event was tracked
// between here and the beginning of this method
// in that case the session is started in logEvent()
if (isEnteringForeground) {
startNewSessionIfNeeded(timestamp);
}
isEnteringForeground = false;
}
});
}
Expand Down

0 comments on commit b0f4fea

Please sign in to comment.