has anyone implemented app idle timeout using kotl...
# android
d
has anyone implemented app idle timeout using kotlin and have an implementation strategy they want to share/highlight?
đŸ˜¶ 2
K 1
g
what is app idle timeout?
g
You can just use it as it is in Java, IDE will convert it for you But Kotlin way for this task would be to use coroutines and particularly delay() from Kotlinx.coroutines: So essentiallly something like this (if convert it as :
Copy code
private val logoutJob: Job? = null
private val coroutineScope: CoroutineScope = TODO() // It can be GlobalScope in this case, depends on when you want this logout work
fun startLogoutTimer() {
    logoutJob.cancel()
    logoutJob = coroutineScope.launch {
       delay(LOGOUT_TIME)
       withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
          // Body of your doInBackground method of AsyncTasks
       }
    }
    
}
If I would implement something like this I would probably instead implement it on base of Flow, though it’s a bit more complicated to explain and code will be much more different from this example In general, it’s quite a big topic, I see that you still use asynctask there, which also really should be rewritten to coroutines