Skip to content

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.

Notifications You must be signed in to change notification settings

angatiabenson/idle-detector-compose

Repository files navigation

Idle Detector Compose 🕒

Maven Central
License
Android API

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.

Features ✨

  • 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

Installation 📦

Add to your build.gradle.kts:

dependencies {  
    implementation("io.github.angatiabenson:idle-detector-compose:0.0.2") 
}  

Basic Usage 🚀

1. Wrap Your App

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()
                }
            }
        }
    }
}

2. Observe State in Composables

@Composable  
fun HomeScreen() {  
    val isIdle by LocalIdleDetectorState.current.collectAsState()  
     if (isIdle) {  
        IdleWarningDialog() 
     }  
    // Your screen content 
}  

3. Reset Idle Timer from Dialogs or Popups

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")
            }
        }
    )
}

API Reference 📚

IdleDetectorProvider Parameters

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

Troubleshooting 🔍

Common Issues

  1. Timeout not triggering
  • Ensure checkInterval is shorter than timeout
  • Verify activity isn't being paused unexpectedly
  1. State not updating
  • Check you're using collectAsState() on LocalIdleDetectorState.current
  1. Multiple callbacks
  • Wrap your onIdle logic in LaunchedEffect if navigation involved

Compatibility 🤝

Version Compose Kotlin Min SDK
0.0.2 1.5.0+ 1.9.0+ 21

License 📄

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.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages