Skip to content

Honor stopWithTask when app UI exits#371

Merged
Dev-hwang merged 3 commits intoDev-hwang:masterfrom
deakjahn:stopwithtask-override
Dec 23, 2025
Merged

Honor stopWithTask when app UI exits#371
Dev-hwang merged 3 commits intoDev-hwang:masterfrom
deakjahn:stopwithtask-override

Conversation

@deakjahn
Copy link
Contributor

Description

This PR improves the behavior of stopWithTask by ensuring the foreground service stops reliably when the app UI is exited (e.g. via the Back button).

Background

Relying on Service.onTaskRemoved() is not sufficient for this use case:

  • It is not guaranteed to be called on all devices or Android versions
  • It does not fire for common exit paths such as backing out of the root activity

As a result, a service configured to stop with the app task may continue running after the UI is dismissed.

What this PR changes

  • Introduces an activity lifecycle-based visibility tracker
  • Detects when the last app activity is no longer resumed
  • Stops the foreground service when:
    • ForegroundTaskOptions.stopWithTask == true
    • the app UI has fully exited

The new behavior only applies when stopWithTask is explicitly enabled. Existing manifest-based behavior remains unchanged.

@Dev-hwang Dev-hwang merged commit 2b8bca0 into Dev-hwang:master Dec 23, 2025
3 checks passed
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.

2 participants