Skip to content

Instantly share code, notes, and snippets.

@sagar-viradiya
Created June 10, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagar-viradiya/ffe87f2013ac187f0d593e03f38b2a6d to your computer and use it in GitHub Desktop.
Save sagar-viradiya/ffe87f2013ac187f0d593e03f38b2a6d to your computer and use it in GitHub Desktop.
Observing livedata permissions
override fun setupObserver(permissionResultLiveData: LiveData<PermissionResult>) {
permissionResultLiveData.observe(this, Observer<PermissionResult> {
when (it) {
is PermissionResult.PermissionGranted -> {
if (it.requestId == REQUEST_ID) {
//Add your logic here after user grants permission(s)
}
}
is PermissionResult.PermissionDenied -> {
if (it.requestId == REQUEST_ID) {
//Add your logic to handle permission denial
}
}
is PermissionResult.PermissionDeniedPermanently -> {
if (it.requestId == REQUEST_ID) {
//Add your logic here if user denied permission(s) permanently.
//Ideally you should ask user to manually go to settings and enable permission(s)
}
}
is PermissionResult.ShowRational -> {
if (it.requestId == REQUEST_ID) {
//If user denied permission frequently then she/he is not clear about why you are asking this permission.
//This is your chance to explain them why you need permission.
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment