0
0
Android Kotlinmobile~10 mins

Activity results 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 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'
AActivityResultContracts.StartActivityForResult()
BActivityResultContracts.GetContent()
CActivityResultContracts.TakePicture()
DActivityResultContracts.RequestPermission()
Attempts:
3 left
💡 Hint
Common Mistakes
Using StartActivityForResult() which returns an ActivityResult, not a Uri.
Using RequestPermission() which is for permissions, not content.
2fill in blank
medium

Complete 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'
A"application/pdf"
B"text/plain"
C"video/*"
D"image/*"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/plain' which is for text files.
Using 'video/*' which is for videos.
3fill in blank
hard

Fix 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'
Auri
Bintent
Cresult
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'intent' or 'data' which are used in the old API.
Using 'result' which is ambiguous.
4fill in blank
hard

Fill 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'
Adata
Bintent
Cresult
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in parameter and super call.
Using 'intent' or 'result' which are not consistent here.
5fill in blank
hard

Fill 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'
A"Pick Image"
B"Pick Video"
C"Pick Audio"
D"Pick Document"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up descriptions or using unrelated strings.
Using 'Pick Document' which is not in the request codes.