Bird
Raised Fist0
Android Kotlinmobile~10 mins

Permission request flow in Android Kotlin - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What is the first step before requesting a dangerous permission in Android Kotlin?
easy
A. Restart the app
B. Directly call requestPermissions without checking
C. Show a Toast message to the user
D. Check if the permission is already granted

Solution

  1. Step 1: Understand permission checking

    Before asking for permission, the app must check if it already has it to avoid unnecessary requests.
  2. Step 2: Use ContextCompat.checkSelfPermission

    This method returns if the permission is granted or denied, guiding the next action.
  3. Final Answer:

    Check if the permission is already granted -> Option D
  4. Quick Check:

    Check permission first = A [OK]
Hint: Always check permission status before requesting [OK]
Common Mistakes:
  • Requesting permission without checking first
  • Assuming permission is granted by default
  • Ignoring permission status before action
2. Which Kotlin code snippet correctly requests a permission from an Activity?
easy
A. ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 100)
B. requestPermissions(Manifest.permission.CAMERA, 100)
C. Activity.requestPermissions(this, Manifest.permission.CAMERA, 100)
D. requestPermission(Manifest.permission.CAMERA)

Solution

  1. Step 1: Identify correct method signature

    ActivityCompat.requestPermissions requires context, array of permissions, and request code.
  2. Step 2: Check syntax correctness

    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 100) matches the correct syntax with 'this' as context and arrayOf for permissions.
  3. Final Answer:

    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 100) -> Option A
  4. Quick Check:

    Correct requestPermissions syntax = D [OK]
Hint: Use ActivityCompat.requestPermissions with arrayOf permissions [OK]
Common Mistakes:
  • Passing single permission string instead of array
  • Using wrong method name or class
  • Missing context parameter
3. Given this code snippet inside an Activity:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  if (requestCode == 101 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    println("Permission granted")
  } else {
    println("Permission denied")
  }
}
What will be printed if the user denies the permission?
medium
A. Permission denied
B. Permission granted
C. No output
D. App crashes

Solution

  1. Step 1: Analyze grantResults array

    If the user denies permission, grantResults[0] will not equal PERMISSION_GRANTED.
  2. Step 2: Follow conditional logic

    The else branch executes, printing "Permission denied".
  3. Final Answer:

    Permission denied -> Option A
  4. Quick Check:

    User denies permission = "Permission denied" [OK]
Hint: Denied permission triggers else branch in onRequestPermissionsResult [OK]
Common Mistakes:
  • Assuming grantResults[0] is always granted
  • Not checking if grantResults is empty
  • Ignoring requestCode check
4. What is wrong with this permission request code snippet?
ActivityCompat.requestPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION, 200)
medium
A. Request code 200 is invalid
B. Missing arrayOf() around permission string
C. Should use ContextCompat instead of ActivityCompat
D. No need to request ACCESS_FINE_LOCATION permission

Solution

  1. Step 1: Check method parameters

    requestPermissions expects an array of permissions, not a single string.
  2. Step 2: Identify correct usage

    Permission string must be wrapped in arrayOf() to form an array.
  3. Final Answer:

    Missing arrayOf() around permission string -> Option B
  4. Quick Check:

    Permissions must be in arrayOf() [OK]
Hint: Wrap permissions in arrayOf() when requesting [OK]
Common Mistakes:
  • Passing single permission string instead of array
  • Confusing ContextCompat with ActivityCompat
  • Using invalid request codes
5. You want to request both CAMERA and LOCATION permissions at once. Which code correctly handles the request and checks the result in Kotlin?
hard
A. requestPermissions(arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 300) override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { println("Permissions handled") }
B. ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 300) override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { if (requestCode == 300 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { println("All permissions granted") } else { println("Some permissions denied") } }
C. ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 300) override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { if (requestCode == 300 && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { println("All permissions granted") } else { println("Some permissions denied") } }
D. ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 300) override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { if (requestCode == 300 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { println("All permissions granted") } else { println("Some permissions denied") } }

Solution

  1. Step 1: Request multiple permissions correctly

    Use arrayOf with both permissions and a unique request code.
  2. Step 2: Check all permissions granted in onRequestPermissionsResult

    Use grantResults.all { it == PackageManager.PERMISSION_GRANTED } to verify all permissions.
  3. Final Answer:

    Request both permissions and check all granted with grantResults.all -> Option C
  4. Quick Check:

    Multiple permissions need all granted check [OK]
Hint: Check all grantResults for multiple permissions [OK]
Common Mistakes:
  • Checking only first permission result
  • Not using arrayOf for multiple permissions
  • Ignoring requestCode in result handler
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard