Skip to content

fix(android): defer wakelock toggle when no activity is attached W…#134

Open
sadaqatdev wants to merge 1 commit into
fluttercommunity:mainfrom
sadaqatdev:main
Open

fix(android): defer wakelock toggle when no activity is attached W…#134
sadaqatdev wants to merge 1 commit into
fluttercommunity:mainfrom
sadaqatdev:main

Conversation

@sadaqatdev
Copy link
Copy Markdown

@sadaqatdev sadaqatdev commented May 21, 2026

…akelock.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 #131, #102, #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 #131, #102, #6.

…lock.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,3 +1,6 @@
## [1.6.2]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not eagerly increment the changelog.

I'll handle deploying the app when the PR is merged.

Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
Android, iOS, macOS, Windows, Linux, and web.
version: 1.6.1
version: 1.6.2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not eagerly increment the pubspec.

I'll handle deploying the app when the PR is merged.

@@ -6,34 +6,35 @@ import android.app.Activity
import android.view.WindowManager

internal class Wakelock {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's a way to create native tests for this change, that would be ideal.

Given that this requires destroying an activity in order to simulate going to the background, you'll probably need something like robolectric for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PlatformException(NoActivityException, dev.fluttercommunity.plus.wakelock.NoActivityException: wakelock requires a foreground activity, C

2 participants