Skip to content

Commit

Permalink
perf: onBackPressed (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Aug 17, 2024
1 parent b712081 commit 4389732
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.core.AnimationConstants
import androidx.compose.runtime.CompositionLocalProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
Expand Down Expand Up @@ -51,7 +52,7 @@ class MainActivity : CompositionActivity({

lifecycleScope.launch {
storeFlow.map(lifecycleScope) { s -> s.excludeFromRecents }.collect {
(app.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).let { manager ->
(app.getSystemService(ACTIVITY_SERVICE) as ActivityManager).let { manager ->
manager.appTasks.forEach { task ->
task?.setExcludeFromRecents(it)
}
Expand Down Expand Up @@ -119,6 +120,18 @@ class MainActivity : CompositionActivity({
super.onStop()
activityVisibleFlow.update { it - 1 }
}

var lastBackPressedTime = 0L
@Suppress("OVERRIDE_DEPRECATION")
override fun onBackPressed() {
// onBackPressedDispatcher.addCallback is not work, it will be covered by compose navigation
val t = System.currentTimeMillis()
if (t - lastBackPressedTime > AnimationConstants.DefaultDurationMillis) {
lastBackPressedTime = t
@Suppress("DEPRECATION")
super.onBackPressed()
}
}
}

private val activityVisibleFlow by lazy { MutableStateFlow(0) }
Expand Down

0 comments on commit 4389732

Please sign in to comment.