André Kindwall
03/12/2021, 9:08 AMandroid:keepScreenOn
?knthmn
03/12/2021, 9:29 AMLocalContext
, and use DisposableEffect()
to create a composable that when in the composition tree, prevents the screen from being turned off.
@Composable
fun KeepScreenOn() {
val activity = findFromContext<Activity>()!!
val flag = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
DisposableEffect(activity) {
activity.window.addFlags(flag)
onDispose {
activity.window.clearFlags(flag)
}
}
}
@Composable
inline fun <reified T> findFromContext(): T? {
var context = LocalContext.current
while (context !is T) {
context = (context as? ContextWrapper)?.baseContext ?: return null
}
return context
}
André Kindwall
03/12/2021, 9:41 AM@Composable
fun KeepScreenOn(content: @Composable () -> Unit) {
val window = LocalWindow.current
DisposableEffect(window) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
onDispose {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
content()
}
Adam Powell
03/12/2021, 2:51 PMChuck Stein
08/12/2021, 8:04 PMLocalWindow
something you created yourself? I don't see it documented anywhere, but would love a way to access the window from a Composable!knthmn
08/13/2021, 4:24 AMWindow
from LocalContext
.Chuck Stein
08/13/2021, 6:47 AMknthmn
08/13/2021, 7:48 AMBackHandler
for searching OnBackPressedDispatcherOwner
from the context
.
https://androidx.tech/artifacts/activity/activity-compose/1.3.0-alpha05-source/androidx/activity/compose/BackHandler.kt.html
https://androidx.tech/artifacts/activity/activity-compose/1.3.0-beta01-source/androidx/activity/compose/ActivityComposeUtils.kt.html
If you use your composables from a fragment you need to handle it separately.knthmn
08/13/2021, 7:50 AMWindow
as a composition local