Package 

Class SessionManager


  • 
    public class SessionManager
    
                        

    Tracks the current {@code #session_id} using Amplitude's session gap rule. The id is a UUID v7 string. Its first 48 bits are the session start time in milliseconds. A new id is assigned when the gap since the last event reaches the configured timeout, or when the account identity changes. App foreground/background transitions do not affect the event gap.

    • Method Summary

      Modifier and Type Method Description
      synchronized String getSessionId()
      static SessionManager getInstance(String instanceKey, SessionStore store) Returns the session manager for an SDK instance.
      synchronized String onEvent(long now, long timeoutMillis) Assigns or extends the session for an event processed at {@code now}.
      synchronized void onEnterBackground(long now) Handles an app background transition without changing session state.Lifecycle transitions are not events and therefore must not extend the session timeout.
      synchronized String onEnterForeground(long now, long timeoutMillis) Handles an app foreground transition without changing session state.
      synchronized void onAccountChanged() Invalidates the current session after the account identity changes.The next event starts a new session; no session is created by the account operation itself.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SessionManager

        SessionManager(SessionStore store)
        Creates a session manager.
        Parameters:
        store - persistence, or null to keep state in memory only
    • Method Detail

      • getInstance

         static SessionManager getInstance(String instanceKey, SessionStore store)

        Returns the session manager for an SDK instance. The first caller supplies persistence;later callers with the same key share that instance.

        Parameters:
        instanceKey - SDK instance name
        store - persistence used when this key has no manager yet; ignored when one already exists
      • onEvent

         synchronized String onEvent(long now, long timeoutMillis)

        Assigns or extends the session for an event processed at {@code now}.

        Parameters:
        now - event processing time in epoch milliseconds
        timeoutMillis - session timeout; values <= 0 use DEFAULT_TIMEOUT_MILLIS
      • onEnterBackground

         synchronized void onEnterBackground(long now)

        Handles an app background transition without changing session state.Lifecycle transitions are not events and therefore must not extend the session timeout.

        Parameters:
        now - background time in epoch milliseconds; ignored for session timing
      • onEnterForeground

         synchronized String onEnterForeground(long now, long timeoutMillis)

        Handles an app foreground transition without changing session state.A timed-out session is rotated only when the next event is processed by onEvent.

        Parameters:
        now - foreground time in epoch milliseconds; ignored for session timing
        timeoutMillis - session timeout; ignored until the next event
      • onAccountChanged

         synchronized void onAccountChanged()

        Invalidates the current session after the account identity changes.The next event starts a new session; no session is created by the account operation itself.