diff --git a/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt b/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt index a90ae09..5b4970e 100644 --- a/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt +++ b/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt @@ -204,10 +204,14 @@ class ForegroundService : Service() { if (::foregroundServiceStatus.isInitialized) { isCorrectlyStopped = foregroundServiceStatus.isCorrectlyStopped() } - val allowAutoRestart = foregroundTaskOptions.allowAutoRestart - if (allowAutoRestart && !isCorrectlyStopped && !ForegroundServiceUtils.isSetStopWithTaskFlag(this)) { - Log.e(TAG, "The service will be restarted after 5 seconds because it wasn't properly stopped.") - RestartReceiver.setRestartAlarm(this, 5000) + + // Safely handle auto-restart by checking if options are initialized. + if (::foregroundTaskOptions.isInitialized) { + val allowAutoRestart = foregroundTaskOptions.allowAutoRestart + if (allowAutoRestart && !isCorrectlyStopped && !ForegroundServiceUtils.isSetStopWithTaskFlag(this)) { + Log.e(TAG, "The service will be restarted after 5 seconds because it wasn't properly stopped.") + RestartReceiver.setRestartAlarm(this, 5000) + } } }