0
0
Android Kotlinmobile~10 mins

Permission request flow in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the app has the CAMERA permission.

Android Kotlin
val hasCameraPermission = ContextCompat.checkSelfPermission(this, [1]) == PackageManager.PERMISSION_GRANTED
Drag options to blanks, or click blank then click option'
AManifest.permission.CAMERA
BManifest.permission.ACCESS_FINE_LOCATION
CManifest.permission.RECORD_AUDIO
DManifest.permission.READ_CONTACTS
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong permission string like location or contacts.
Forgetting to compare the result to PERMISSION_GRANTED.
2fill in blank
medium

Complete the code to request CAMERA permission from the user.

Android Kotlin
ActivityCompat.requestPermissions(this, arrayOf([1]), REQUEST_CAMERA_CODE)
Drag options to blanks, or click blank then click option'
AManifest.permission.CAMERA
BManifest.permission.ACCESS_FINE_LOCATION
CManifest.permission.READ_SMS
DManifest.permission.RECORD_AUDIO
Attempts:
3 left
💡 Hint
Common Mistakes
Requesting a different permission than the one checked.
Not wrapping the permission string inside an array.
3fill in blank
hard

Fix the error in the permission result check inside onRequestPermissionsResult.

Android Kotlin
if (requestCode == REQUEST_CAMERA_CODE && grantResults.isNotEmpty() && grantResults[[1]] == PackageManager.PERMISSION_GRANTED) {
    // Permission granted
}
Drag options to blanks, or click blank then click option'
A1
B-1
C0
DrequestCode
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 or other invalid indices causing crashes.
Comparing with requestCode instead of grantResults.
4fill in blank
hard

Fill both blanks to create a function that checks and requests CAMERA permission if not granted.

Android Kotlin
fun checkCameraPermission() {
    if (ContextCompat.checkSelfPermission(this, [1]) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, arrayOf([2]), REQUEST_CAMERA_CODE)
    }
}
Drag options to blanks, or click blank then click option'
AManifest.permission.CAMERA
BManifest.permission.ACCESS_FINE_LOCATION
DManifest.permission.READ_CONTACTS
Attempts:
3 left
💡 Hint
Common Mistakes
Using different permissions in check and request.
Forgetting to wrap the permission in an array when requesting.
5fill in blank
hard

Fill all three blanks to handle permission result and show a message if permission is denied.

Android Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == REQUEST_CAMERA_CODE) {
        if (grantResults.isNotEmpty() && grantResults[[1]] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, [2], Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, [3], Toast.LENGTH_SHORT).show()
        }
    }
}
Drag options to blanks, or click blank then click option'
A0
B"Camera permission granted"
C"Camera permission denied"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index for grantResults.
Mixing up granted and denied messages.
Forgetting to call super method.