I consider myself a beginner regarding Kotlin, so hopefully it is just me that is doing something wrong, but I have been struggling for several days to try to get this LiveData to actually work, but without success.
In the end, I built this app and just added some extra logging in the listener class to see when it actually reacts to permission changes:
class PermissionStatusListener(private val context: Context,
private val permissionToListen: String) : LiveData<PermissionStatus>() {
private val TAG = "* Permission listener "
override fun onActive() = handlePermissionCheck()
private fun handlePermissionCheck() {
val isPermissionGranted = ActivityCompat.checkSelfPermission(context,
permissionToListen) == PackageManager.PERMISSION_GRANTED
Log.d(TAG, " permission status ${isPermissionGranted}")
if (isPermissionGranted)
postValue(PermissionStatus.Granted())
else
postValue(PermissionStatus.Denied())
}
}
sealed class PermissionStatus {
data class Granted(
val message: Int = R.string.permission_status_granted
) : PermissionStatus()
data class Denied(
val message: Int = R.string.permission_status_denied
) : PermissionStatus()
data class Blocked(
val message: Int = R.string.permission_status_blocked
) : PermissionStatus()
}
When the app starts, it shows the correct status of the permission:
2019-12-21 21:55:04.668 11799-11799/com.wahibhaq.locationservicelivedata D/* Permission listener: permission status false
But when the permission is granted, there is no update of the LiveData, only the normal permission handling callbacks:
2019-12-21 22:20:47.899 11799-11799/com.wahibhaq.locationservicelivedata D/Permissions: No rationale.
2019-12-21 22:20:47.939 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: setView = DecorView@b80bcc5[PermissionsActivity] TM=true MM=false
2019-12-21 22:20:47.956 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: dispatchAttachedToWindow
2019-12-21 22:20:47.969 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Relayout returned: old=[0,63][1080,2094] new=[540,1078][540,1078] result=0x7 surface={valid=true 532950945792} changed=true
2019-12-21 22:20:47.976 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_RESIZED: frame=Rect(540, 1078 - 540, 1078) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
2019-12-21 22:20:51.313 11799-11799/com.wahibhaq.locationservicelivedata D/Permissions: Just allowed.
2019-12-21 22:20:51.363 11799-11799/com.wahibhaq.locationservicelivedata I/MainActivity$permissionHandler: Activity: 2131427403
2019-12-21 22:20:51.381 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2019-12-21 22:20:51.381 11799-11799/com.wahibhaq.locationservicelivedata D/InputMethodManager: prepareNavigationBarInfo() DecorView@b80bcc5[PermissionsActivity]
2019-12-21 22:20:51.413 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_WINDOW_FOCUS_CHANGED 0 0
2019-12-21 22:20:51.428 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: setWindowStopped(true) old=false
2019-12-21 22:20:51.433 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: dispatchDetachedFromWindow
2019-12-21 22:20:51.434 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Surface release. android.view.ViewRootImpl.doDie:7979 android.view.ViewRootImpl.die:7947 android.view.WindowManagerGlobal.removeViewLocked:497 android.view.WindowManagerGlobal.removeView:435 android.view.WindowManagerImpl.removeViewImmediate:124 android.app.ActivityThread.handleDestroyActivity:4753 android.app.servertransaction.DestroyActivityItem.execute:39 android.app.servertransaction.TransactionExecutor.executeLifecycleState:145
2019-12-21 22:20:51.644 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Surface release. android.view.ViewRootImpl$ViewRootHandler.handleMessage:4825 android.os.Handler.dispatchMessage:106
To me it seems that the LiveData is only updated when the observing activity is resumed, and not directly when the permission is granted.
Am I missing something obvious, or does the Android framework not allow this listener to see the changed permission and update the LiveData immediately?