A Jetpack Compose library that detects user inactivity across your entire app with zero boilerplate. Perfect for implementing session timeouts, security screens, or automatic logouts.
- Global Activity Monitoring - Works across all screens
- Lifecycle Aware - Automatically pauses/resumes with activity
- Customizable Timeouts - Set duration in minutes, seconds, or milliseconds
- Compose Native - Built with 100% Jetpack Compose
- Touch Interaction Detection - Captures all user interactions
- State Management - Observable idle state via CompositionLocal
Add to your build.gradle.kts
:
dependencies {
implementation("io.github.angatiabenson:idle-detector-compose:0.0.2")
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
IdleDetectorProvider(
idleTimeout = 5.seconds,
checkInterval = 1.seconds,
onIdle = { /* Could show dialog or navigate here */ },
){
IdledetectorappTheme {
AppContent()
}
}
}
}
}
@Composable
fun HomeScreen() {
val isIdle by LocalIdleDetectorState.current.collectAsState()
if (isIdle) {
IdleWarningDialog()
}
// Your screen content
}
The new LocalIdleReset
CompositionLocal allows you to reset the idle timer manually. For example, in a dialog:
@Composable
fun TestDialog(onDismiss: () -> Unit) {
// Capture the idle reset lambda from the CompositionLocal
val idleReset = LocalIdleReset.current
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Test Dialog") },
text = { Text("Click confirm to reset idle timer.") },
confirmButton = {
Button(
onClick = {
// Invoke the idle reset lambda
idleReset?.invoke()
onDismiss()
}
) {
Text("Confirm")
}
},
dismissButton = {
OutlinedButton(
onClick = onDismiss
) {
Text("Dismiss")
}
}
)
}
Parameter | Type | Default | Description |
---|---|---|---|
timeout |
Duration |
Required | Duration until idle state triggers |
onIdle |
() -> Unit |
Required | Callback when idle state is reached |
checkInterval |
Duration |
1.second | How often to check for inactivity |
content |
@Composable |
Required | Your app content |
- Timeout not triggering
- Ensure
checkInterval
is shorter thantimeout
- Verify activity isn't being paused unexpectedly
- State not updating
- Check you're using
collectAsState()
onLocalIdleDetectorState.current
- Multiple callbacks
- Wrap your
onIdle
logic inLaunchedEffect
if navigation involved
Version | Compose | Kotlin | Min SDK |
---|---|---|---|
0.0.2 | 1.5.0+ | 1.9.0+ | 21 |
Copyright 2025 Angatia Benson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Happy coding! 🎉 If you encounter any issues, please open an issue.