FileOperationService: remove the correct notification helper on activity destroy

This commit is contained in:
Matéo Duparc 2024-11-13 11:52:42 +01:00
parent 2d5f5a82c9
commit 07f5f8b5d9
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
2 changed files with 4 additions and 2 deletions

View File

@ -104,7 +104,9 @@ class FileOperationService : Service() {
activity.lifecycle.addObserver(object : DefaultLifecycleObserver { activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) { override fun onDestroy(owner: LifecycleOwner) {
activity.unbindService(serviceConnection) 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( activity.bindService(

View File

@ -28,7 +28,7 @@ object AndroidUtils {
/** /**
* A [Manifest.permission.POST_NOTIFICATIONS] permission helper. * A [Manifest.permission.POST_NOTIFICATIONS] permission helper.
* *
* Must be initialized before [Activity.onCreate]. * Must be initialized before [Activity.onCreate] finishes.
*/ */
class NotificationPermissionHelper<out A: AppCompatActivity>(val activity: A) { class NotificationPermissionHelper<out A: AppCompatActivity>(val activity: A) {
private var listener: ((Boolean) -> Unit)? = null private var listener: ((Boolean) -> Unit)? = null