From 07f5f8b5d9ff20fcf88bb870edbfc56b51269ae7 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Wed, 13 Nov 2024 11:52:42 +0100 Subject: [PATCH] FileOperationService: remove the correct notification helper on activity destroy --- .../hardcore/droidfs/file_operations/FileOperationService.kt | 4 +++- app/src/main/java/sushi/hardcore/droidfs/util/AndroidUtils.kt | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt index 4cc66d2..c446f3d 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt @@ -104,7 +104,9 @@ class FileOperationService : Service() { activity.lifecycle.addObserver(object : DefaultLifecycleObserver { override fun onDestroy(owner: LifecycleOwner) { activity.unbindService(serviceConnection) - service.notificationPermissionHelpers.removeLast() + // Could have been more efficient with a LinkedHashMap but the JDK implementation doesn't allow + // to access the latest element in O(1) unless using reflection + service.notificationPermissionHelpers.removeAll { it.activity == activity } } }) activity.bindService( diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/AndroidUtils.kt b/app/src/main/java/sushi/hardcore/droidfs/util/AndroidUtils.kt index 7bd6509..b92e21f 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/AndroidUtils.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/AndroidUtils.kt @@ -28,7 +28,7 @@ object AndroidUtils { /** * A [Manifest.permission.POST_NOTIFICATIONS] permission helper. * - * Must be initialized before [Activity.onCreate]. + * Must be initialized before [Activity.onCreate] finishes. */ class NotificationPermissionHelper(val activity: A) { private var listener: ((Boolean) -> Unit)? = null