Created
June 10, 2019 18:28
-
-
Save sagar-viradiya/ffe87f2013ac187f0d593e03f38b2a6d to your computer and use it in GitHub Desktop.
Observing livedata permissions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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