Skip to content

Commit 2fb30ca

Browse files
committed
fix(android): defer wakelock toggle when no activity is attached Wakelock.toggle()/isEnabled() threw NoActivityException whenever they were called with no foreground activity attached (e.g. the app is backgrounded or mid lifecycle transition), surfacing as non-fatal crashes in apps that toggle the wakelock off the foreground path. Instead of throwing, the requested wakelock state is now tracked in Kotlin and re-applied once an activity (re)attaches — matching how the Windows/Linux/web implementations track state. The activity setter only re-asserts FLAG_KEEP_SCREEN_ON when wakelock was actually enabled, so it won't clear the flag for apps that set it on their own window. The now-unused NoActivityException class is removed. Bumps wakelock_plus to 1.6.2 and updates the CHANGELOG. Fixes fluttercommunity#131, fluttercommunity#102, fluttercommunity#6.
fix(android): defer wakelock toggle when no activity is attached Wakelock.toggle()/isEnabled() threw NoActivityException whenever they were called with no foreground activity attached (e.g. the app is backgrounded or mid lifecycle transition), surfacing as non-fatal crashes in apps that toggle the wakelock off the foreground path. Instead of throwing, the requested wakelock state is now tracked in Kotlin and re-applied once an activity (re)attaches — matching how the Windows/Linux/web implementations track state. The activity setter only re-asserts FLAG_KEEP_SCREEN_ON when wakelock was actually enabled, so it won't clear the flag for apps that set it on their own window. The now-unused NoActivityException class is removed. Bumps wakelock_plus to 1.6.2 and updates the CHANGELOG. Fixes fluttercommunity#131, fluttercommunity#102, fluttercommunity#6.
1 parent 4f6ee71 commit 2fb30ca

4 files changed

Lines changed: 30 additions & 25 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
.buildlog/
99
.history
1010
.svn/
11-
.metadata
11+
.metadata
12+
CLAUDE.md

‎wakelock_plus/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [1.6.2]
2+
* Android: Fixed `NoActivityException` ("wakelock requires a foreground activity") being thrown when `toggle`/`enabled` were called with no foreground activity attached (e.g. while the app is backgrounded or during a lifecycle transition). The requested wakelock state is now remembered and re-applied once an activity (re)attaches instead of throwing.
3+
14
## [1.6.1]
25
* [#133](https://github.com/fluttercommunity/wakelock_plus/pull/133): wakelock_plus Flutter 3.38 downgrade. Thanks [diegotori](https://github.com/diegotori).
36
- Library now requires Dart version `3.10` or higher, restoring previous compatibility.

‎wakelock_plus/android/src/main/kotlin/dev/fluttercommunity/plus/wakelock/Wakelock.kt‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,35 @@ import android.app.Activity
66
import android.view.WindowManager
77

88
internal class Wakelock {
9-
var activity: Activity? = null
10-
11-
private val enabled
12-
get() = activity!!.window.attributes.flags and
13-
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON != 0
9+
// The desired wakelock state. Tracked independently of [activity] so that a
10+
// toggle requested while no activity is attached (e.g. the app is in the
11+
// background or mid lifecycle transition) is remembered and re-applied once an
12+
// activity (re)attaches, instead of throwing a NoActivityException.
13+
private var enableWakelock = false
1414

15-
fun toggle(message: ToggleMessage) {
16-
if (activity == null) {
17-
throw NoActivityException()
15+
var activity: Activity? = null
16+
set(value) {
17+
field = value
18+
// Re-assert the wakelock on the newly attached activity's window, but only
19+
// when it was actually requested. If the user never enabled it (or last
20+
// disabled it), leave the flag alone — the activity may keep the screen on
21+
// for its own reasons.
22+
if (enableWakelock) applyWakelock()
1823
}
1924

20-
val activity = this.activity!!
21-
val enabled = this.enabled
22-
23-
if (message.enable!!) {
24-
if (!enabled) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
25-
} else if (enabled) {
26-
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
25+
private fun applyWakelock() {
26+
val window = activity?.window ?: return
27+
if (enableWakelock) {
28+
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
29+
} else {
30+
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
2731
}
2832
}
2933

30-
fun isEnabled(): IsEnabledMessage {
31-
if (activity == null) {
32-
throw NoActivityException()
33-
}
34-
35-
return IsEnabledMessage(enabled = enabled)
34+
fun toggle(message: ToggleMessage) {
35+
enableWakelock = message.enable!!
36+
applyWakelock()
3637
}
37-
}
3838

39-
class NoActivityException : Exception("wakelock requires a foreground activity")
39+
fun isEnabled(): IsEnabledMessage = IsEnabledMessage(enabled = enableWakelock)
40+
}

‎wakelock_plus/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: wakelock_plus
22
description: >-2
33
Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
44
Android, iOS, macOS, Windows, Linux, and web.
5-
version: 1.6.1
5+
version: 1.6.2
66
repository: https://github.com/fluttercommunity/wakelock_plus/tree/main/wakelock_plus
77

88
environment:

0 commit comments

Comments
 (0)