Skip to content

Conversation

@udyan-dev
Copy link

Description

This PR resolves a critical UninitializedPropertyAccessException that occurs when the ForegroundService is destroyed by the Android OS before the foregroundTaskOptions property has been initialized.

Under certain conditions (e.g., the system reclaiming resources while the app is in the background), onDestroy() can be called without onStartCommand() having run, leading to a crash as the lateinit property is accessed before it has a value.

The Fix

The solution is to add a defensive, idiomatic Kotlin check (::foregroundTaskOptions.isInitialized) before accessing the property in the onDestroy method. This ensures the app does not crash in this edge case and safely skips the auto-restart logic when the service's options were never loaded.

Verification

This change is a standard, low-risk solution for this common Android/Kotlin lifecycle issue.

  • If foregroundTaskOptions is initialized, the behavior is unchanged.
  • If it is not initialized, the crash is prevented.

This directly addresses the root cause of the crash reports related to lateinit property foregroundTaskOptions has not been initialized.

Resolves an UninitializedPropertyAccessException that occurs when the ForegroundService is destroyed by the OS before foregroundTaskOptions has been initialized.

This change adds a defensive check using '::foregroundTaskOptions.isInitialized' before accessing the property in the onDestroy method, ensuring the app does not crash in this edge case.
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.

1 participant