Monitor the call session lifecycle with SessionStatusListener. This listener provides callbacks for session join/leave events, connection status changes, and session timeouts.
Prerequisites
Register Listener
Register a SessionStatusListener to receive session status callbacks:
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")
}
})
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");
}
});
The listener is automatically removed when the LifecycleOwner (Activity/Fragment) is destroyed, preventing memory leaks.
Callbacks
onSessionJoined
Triggered when you successfully join a call session.
override fun onSessionJoined() {
Log.d(TAG, "Successfully joined the session")
// Update UI to show call screen
// Start any call-related services
}
@Override
public void onSessionJoined() {
Log.d(TAG, "Successfully joined the session");
// Update UI to show call screen
// Start any call-related services
}
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).
override fun onSessionLeft() {
Log.d(TAG, "Left the session")
// Clean up resources
// Navigate back to previous screen
finish()
}
@Override
public void onSessionLeft() {
Log.d(TAG, "Left the session");
// Clean up resources
// Navigate back to previous screen
finish();
}
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).
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()
}
@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();
}
Configure the timeout period using setIdleTimeoutPeriod() in SessionSettings. Default is 300 seconds (5 minutes).
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).
override fun onConnectionLost() {
Log.d(TAG, "Connection lost - attempting to reconnect")
// Show reconnecting indicator
showReconnectingUI()
}
@Override
public void onConnectionLost() {
Log.d(TAG, "Connection lost - attempting to reconnect");
// Show reconnecting indicator
showReconnectingUI();
}
Use Cases:
- Display “Reconnecting…” indicator
- Disable call controls temporarily
- Log connection issue for debugging
onConnectionRestored
Triggered when the connection is restored after being lost.
override fun onConnectionRestored() {
Log.d(TAG, "Connection restored")
// Hide reconnecting indicator
hideReconnectingUI()
// Re-enable call controls
}
@Override
public void onConnectionRestored() {
Log.d(TAG, "Connection restored");
// Hide reconnecting indicator
hideReconnectingUI();
// Re-enable call controls
}
Use Cases:
- Hide reconnecting indicator
- Re-enable call controls
- Show success notification
onConnectionClosed
Triggered when the connection is permanently closed (cannot be restored).
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()
}
@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();
}
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:
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"
}
}
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
}
}
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