> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-v6-beta2-flutter-uikit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Session Status Listener

Monitor the call session lifecycle with `SessionStatusListener`. This listener provides callbacks for session join/leave events, connection status changes, and session timeouts.

## Prerequisites

* An active [call session](/calls/android/join-session)
* Access to the `CallSession` instance

## Register Listener

Register a `SessionStatusListener` to receive session status callbacks:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    val callSession = CallSession.getInstance()

    callSession.addSessionStatusListener(this, object : SessionStatusListener() {
        override fun onSessionJoined() {
            Log.d(TAG, "Successfully joined the session")
        }

        override fun onSessionLeft() {
            Log.d(TAG, "Left the session")
        }

        override fun onSessionTimedOut() {
            Log.d(TAG, "Session timed out")
        }

        override fun onConnectionLost() {
            Log.d(TAG, "Connection lost")
        }

        override fun onConnectionRestored() {
            Log.d(TAG, "Connection restored")
        }

        override fun onConnectionClosed() {
            Log.d(TAG, "Connection closed")
        }
    })
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    CallSession callSession = CallSession.getInstance();

    callSession.addSessionStatusListener(this, new SessionStatusListener() {
        @Override
        public void onSessionJoined() {
            Log.d(TAG, "Successfully joined the session");
        }

        @Override
        public void onSessionLeft() {
            Log.d(TAG, "Left the session");
        }

        @Override
        public void onSessionTimedOut() {
            Log.d(TAG, "Session timed out");
        }

        @Override
        public void onConnectionLost() {
            Log.d(TAG, "Connection lost");
        }

        @Override
        public void onConnectionRestored() {
            Log.d(TAG, "Connection restored");
        }

        @Override
        public void onConnectionClosed() {
            Log.d(TAG, "Connection closed");
        }
    });
    ```
  </Tab>
</Tabs>

<Note>
  The listener is automatically removed when the `LifecycleOwner` (Activity/Fragment) is destroyed, preventing memory leaks.
</Note>

***

## Callbacks

### onSessionJoined

Triggered when you successfully join a call session.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onSessionJoined() {
        Log.d(TAG, "Successfully joined the session")
        // Update UI to show call screen
        // Start any call-related services
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onSessionJoined() {
        Log.d(TAG, "Successfully joined the session");
        // Update UI to show call screen
        // Start any call-related services
    }
    ```
  </Tab>
</Tabs>

**Use Cases:**

* Update UI to display the call interface
* Start foreground service for ongoing call notification
* Initialize call-related features

***

### onSessionLeft

Triggered when you leave the call session (either by calling `leaveSession()` or being removed).

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onSessionLeft() {
        Log.d(TAG, "Left the session")
        // Clean up resources
        // Navigate back to previous screen
        finish()
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onSessionLeft() {
        Log.d(TAG, "Left the session");
        // Clean up resources
        // Navigate back to previous screen
        finish();
    }
    ```
  </Tab>
</Tabs>

**Use Cases:**

* Clean up call-related resources
* Stop foreground service
* Navigate away from call screen

***

### onSessionTimedOut

Triggered when the session times out due to inactivity (e.g., being alone in the call for too long).

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onSessionTimedOut() {
        Log.d(TAG, "Session timed out due to inactivity")
        // Show timeout message to user
        Toast.makeText(this, "Call ended due to inactivity", Toast.LENGTH_SHORT).show()
        finish()
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onSessionTimedOut() {
        Log.d(TAG, "Session timed out due to inactivity");
        // Show timeout message to user
        Toast.makeText(this, "Call ended due to inactivity", Toast.LENGTH_SHORT).show();
        finish();
    }
    ```
  </Tab>
</Tabs>

<Note>
  Configure the timeout period using `setIdleTimeoutPeriod()` in [SessionSettings](/calls/android/session-settings). Default is 300 seconds (5 minutes).
</Note>

**Use Cases:**

* Display timeout notification to user
* Clean up resources and navigate away
* Log analytics event

***

### onConnectionLost

Triggered when the connection to the call server is lost (e.g., network issues).

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onConnectionLost() {
        Log.d(TAG, "Connection lost - attempting to reconnect")
        // Show reconnecting indicator
        showReconnectingUI()
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onConnectionLost() {
        Log.d(TAG, "Connection lost - attempting to reconnect");
        // Show reconnecting indicator
        showReconnectingUI();
    }
    ```
  </Tab>
</Tabs>

**Use Cases:**

* Display "Reconnecting..." indicator
* Disable call controls temporarily
* Log connection issue for debugging

***

### onConnectionRestored

Triggered when the connection is restored after being lost.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onConnectionRestored() {
        Log.d(TAG, "Connection restored")
        // Hide reconnecting indicator
        hideReconnectingUI()
        // Re-enable call controls
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onConnectionRestored() {
        Log.d(TAG, "Connection restored");
        // Hide reconnecting indicator
        hideReconnectingUI();
        // Re-enable call controls
    }
    ```
  </Tab>
</Tabs>

**Use Cases:**

* Hide reconnecting indicator
* Re-enable call controls
* Show success notification

***

### onConnectionClosed

Triggered when the connection is permanently closed (cannot be restored).

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onConnectionClosed() {
        Log.d(TAG, "Connection closed permanently")
        // Show error message
        Toast.makeText(this, "Call connection lost", Toast.LENGTH_SHORT).show()
        // Clean up and exit
        finish()
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    @Override
    public void onConnectionClosed() {
        Log.d(TAG, "Connection closed permanently");
        // Show error message
        Toast.makeText(this, "Call connection lost", Toast.LENGTH_SHORT).show();
        // Clean up and exit
        finish();
    }
    ```
  </Tab>
</Tabs>

**Use Cases:**

* Display error message to user
* Clean up resources
* Navigate away from call screen

***

## Complete Example

Here's a complete example handling all session status events:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    class CallActivity : AppCompatActivity() {
        private lateinit var callSession: CallSession

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_call)

            callSession = CallSession.getInstance()
            setupSessionStatusListener()
        }

        private fun setupSessionStatusListener() {
            callSession.addSessionStatusListener(this, object : SessionStatusListener() {
                override fun onSessionJoined() {
                    Log.d(TAG, "Session joined")
                    runOnUiThread {
                        // Start ongoing call service
                        CometChatOngoingCallService.launch(this@CallActivity)
                    }
                }

                override fun onSessionLeft() {
                    Log.d(TAG, "Session left")
                    runOnUiThread {
                        CometChatOngoingCallService.abort(this@CallActivity)
                        finish()
                    }
                }

                override fun onSessionTimedOut() {
                    Log.d(TAG, "Session timed out")
                    runOnUiThread {
                        Toast.makeText(
                            this@CallActivity,
                            "Call ended due to inactivity",
                            Toast.LENGTH_SHORT
                        ).show()
                        CometChatOngoingCallService.abort(this@CallActivity)
                        finish()
                    }
                }

                override fun onConnectionLost() {
                    Log.d(TAG, "Connection lost")
                    runOnUiThread {
                        showReconnectingOverlay(true)
                    }
                }

                override fun onConnectionRestored() {
                    Log.d(TAG, "Connection restored")
                    runOnUiThread {
                        showReconnectingOverlay(false)
                    }
                }

                override fun onConnectionClosed() {
                    Log.d(TAG, "Connection closed")
                    runOnUiThread {
                        Toast.makeText(
                            this@CallActivity,
                            "Connection lost. Please try again.",
                            Toast.LENGTH_SHORT
                        ).show()
                        CometChatOngoingCallService.abort(this@CallActivity)
                        finish()
                    }
                }
            })
        }

        private fun showReconnectingOverlay(show: Boolean) {
            // Show/hide reconnecting UI overlay
        }

        companion object {
            private const val TAG = "CallActivity"
        }
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    public class CallActivity extends AppCompatActivity {
        private static final String TAG = "CallActivity";
        private CallSession callSession;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_call);

            callSession = CallSession.getInstance();
            setupSessionStatusListener();
        }

        private void setupSessionStatusListener() {
            callSession.addSessionStatusListener(this, new SessionStatusListener() {
                @Override
                public void onSessionJoined() {
                    Log.d(TAG, "Session joined");
                    runOnUiThread(() -> {
                        // Start ongoing call service
                        CometChatOngoingCallService.launch(CallActivity.this);
                    });
                }

                @Override
                public void onSessionLeft() {
                    Log.d(TAG, "Session left");
                    runOnUiThread(() -> {
                        CometChatOngoingCallService.abort(CallActivity.this);
                        finish();
                    });
                }

                @Override
                public void onSessionTimedOut() {
                    Log.d(TAG, "Session timed out");
                    runOnUiThread(() -> {
                        Toast.makeText(
                            CallActivity.this,
                            "Call ended due to inactivity",
                            Toast.LENGTH_SHORT
                        ).show();
                        CometChatOngoingCallService.abort(CallActivity.this);
                        finish();
                    });
                }

                @Override
                public void onConnectionLost() {
                    Log.d(TAG, "Connection lost");
                    runOnUiThread(() -> showReconnectingOverlay(true));
                }

                @Override
                public void onConnectionRestored() {
                    Log.d(TAG, "Connection restored");
                    runOnUiThread(() -> showReconnectingOverlay(false));
                }

                @Override
                public void onConnectionClosed() {
                    Log.d(TAG, "Connection closed");
                    runOnUiThread(() -> {
                        Toast.makeText(
                            CallActivity.this,
                            "Connection lost. Please try again.",
                            Toast.LENGTH_SHORT
                        ).show();
                        CometChatOngoingCallService.abort(CallActivity.this);
                        finish();
                    });
                }
            });
        }

        private void showReconnectingOverlay(boolean show) {
            // Show/hide reconnecting UI overlay
        }
    }
    ```
  </Tab>
</Tabs>

***

## Callbacks Summary

| Callback                 | Description                              |
| ------------------------ | ---------------------------------------- |
| `onSessionJoined()`      | Successfully joined the call session     |
| `onSessionLeft()`        | Left the call session                    |
| `onSessionTimedOut()`    | Session ended due to inactivity timeout  |
| `onConnectionLost()`     | Connection to server lost (reconnecting) |
| `onConnectionRestored()` | Connection restored after being lost     |
| `onConnectionClosed()`   | Connection permanently closed            |

## Next Steps

<CardGroup cols={2}>
  <Card title="Participant Event Listener" icon="users" href="/calls/android/participant-event-listener">
    Handle participant join/leave and state changes
  </Card>

  <Card title="Media Events Listener" icon="video" href="/calls/android/media-events-listener">
    Handle recording, screen share, and media state changes
  </Card>
</CardGroup>
