From 998078bbbc222d305ae9cef7318c004bc2828a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Tue, 25 Oct 2022 12:31:38 +0200 Subject: [PATCH] PermissionUtil: ensure fragment result is listened to on activity thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevents a crash if called from background Signed-off-by: Álvaro Brey --- .../com/owncloud/android/utils/PermissionUtil.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/utils/PermissionUtil.kt b/app/src/main/java/com/owncloud/android/utils/PermissionUtil.kt index 6dde844a96..1821b2b453 100644 --- a/app/src/main/java/com/owncloud/android/utils/PermissionUtil.kt +++ b/app/src/main/java/com/owncloud/android/utils/PermissionUtil.kt @@ -31,6 +31,7 @@ import android.content.pm.PackageManager import android.content.pm.ResolveInfo import android.net.Uri import android.os.Build +import android.os.Bundle import android.os.Environment import android.provider.Settings import androidx.annotation.RequiresApi @@ -213,10 +214,7 @@ object PermissionUtil { if (shouldRequestPermission && activity.supportFragmentManager.findFragmentByTag(PERMISSION_CHOICE_DIALOG_TAG) == null ) { - activity.supportFragmentManager.setFragmentResultListener( - StoragePermissionDialogFragment.REQUEST_KEY, - activity - ) { _, resultBundle -> + val listener: (requestKey: String, result: Bundle) -> Unit = { _, resultBundle -> val result: StoragePermissionDialogFragment.Result? = resultBundle.getParcelable(StoragePermissionDialogFragment.RESULT_KEY) if (result != null) { @@ -238,6 +236,14 @@ object PermissionUtil { } } + activity.runOnUiThread { + activity.supportFragmentManager.setFragmentResultListener( + StoragePermissionDialogFragment.REQUEST_KEY, + activity, + listener + ) + } + val dialogFragment = StoragePermissionDialogFragment.newInstance(permissionRequired) dialogFragment.show(activity.supportFragmentManager, PERMISSION_CHOICE_DIALOG_TAG) }