Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start an activity for result using the new Activity Result API.
Android Kotlin
val getContent = registerForActivityResult([1]) { uri ->
// Handle the returned Uri
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using StartActivityForResult() which returns an ActivityResult, not a Uri.
Using RequestPermission() which is for permissions, not content.
✗ Incorrect
The GetContent() contract lets you pick content and get a Uri result.
2fill in blank
mediumComplete the code to launch the activity to pick an image.
Android Kotlin
getContent.launch([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/plain' which is for text files.
Using 'video/*' which is for videos.
✗ Incorrect
To pick an image, the MIME type should be "image/*".
3fill in blank
hardFix the error in the callback parameter type for the activity result launcher.
Android Kotlin
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { [1] ->
// Use the Uri
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'intent' or 'data' which are used in the old API.
Using 'result' which is ambiguous.
✗ Incorrect
The callback receives a Uri? named here as uri.
4fill in blank
hardFill both blanks to correctly handle the result in the old onActivityResult method.
Android Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, [1]: Intent?) { super.onActivityResult(requestCode, resultCode, [2]) if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) { val uri = data?.data // Use the uri } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in parameter and super call.
Using 'intent' or 'result' which are not consistent here.
✗ Incorrect
The parameter and the super call both use data as the Intent variable.
5fill in blank
hardFill all three blanks to create a map of request codes to their string descriptions.
Android Kotlin
val requestDescriptions = mapOf(
PICK_IMAGE to [1],
PICK_VIDEO to [2],
PICK_AUDIO to [3]
) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up descriptions or using unrelated strings.
Using 'Pick Document' which is not in the request codes.
✗ Incorrect
This map links each request code to a descriptive string for clarity.