What is the purpose of ActivityResultLauncher in Android Kotlin?
It is used to launch another activity and receive a result back asynchronously, replacing the older startActivityForResult method.
Click to reveal answer
beginner
How do you register for an activity result in Android Kotlin?
You use registerForActivityResult() with an ActivityResultContract and a callback to handle the result.
Click to reveal answer
intermediate
What is an ActivityResultContract?
It defines the input type needed to start an activity and the output type returned as a result, helping to standardize communication between activities.
Click to reveal answer
intermediate
Why is the new activity result API preferred over startActivityForResult?
Because it is lifecycle-aware, safer, and easier to manage results without overriding onActivityResult.
Click to reveal answer
beginner
What happens if you try to launch an activity result without registering the launcher first?
The app will crash or the result will not be delivered because the launcher is not initialized to handle the result callback.
Click to reveal answer
Which method replaces startActivityForResult in modern Android Kotlin development?
AlaunchActivity
BstartActivity
ConActivityResult
DregisterForActivityResult
✗ Incorrect
registerForActivityResult is the new recommended way to launch activities for results.
What is the role of the callback passed to registerForActivityResult?
ATo handle the result returned from the launched activity
BTo start the activity
CTo register the activity in the manifest
DTo create a new activity instance
✗ Incorrect
The callback receives the result data when the launched activity finishes.
Which class defines the input and output types for activity results?
ABundle
BIntent
CActivityResultContract
DActivityResultLauncher
✗ Incorrect
ActivityResultContract specifies how to create the intent and parse the result.
What lifecycle benefit does the new activity result API provide?
AIt is lifecycle-aware and avoids memory leaks
BIt runs on a background thread
CIt automatically restarts the activity
DIt disables lifecycle callbacks
✗ Incorrect
The API respects the lifecycle of the component to prevent leaks and crashes.
How do you launch the activity after registering the launcher?
ACall <code>startActivityForResult()</code>
BCall <code>launch()</code> on the <code>ActivityResultLauncher</code>
CCall <code>onActivityResult()</code>
DCall <code>registerForActivityResult()</code> again
✗ Incorrect
You use the launch() method on the registered launcher to start the activity.
Explain how to use the new activity result API in Android Kotlin to get data back from another activity.
Think about how you prepare, start, and receive results from another screen.
You got /4 concepts.
Describe the advantages of the new activity result API compared to the old startActivityForResult method.
Focus on safety and ease of use improvements.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of registerForActivityResult in Android Kotlin?
easy
A. To create a new fragment instance
B. To start a new activity without expecting a result
C. To register a broadcast receiver
D. To safely receive data back from another activity
Solution
Step 1: Understand the role of registerForActivityResult
This function is designed to handle results from activities started for a result safely and simply.
Step 2: Compare with other options
Starting an activity without expecting a result or registering broadcast receivers are unrelated to this function.
Final Answer:
To safely receive data back from another activity -> Option D
Quick Check:
Activity result handling = A [OK]
Hint: Remember: registerForActivityResult handles returned data safely [OK]
Common Mistakes:
Confusing it with starting activities without results
Thinking it registers broadcast receivers
Assuming it creates fragments
2. Which of the following is the correct syntax to register an activity result launcher in Kotlin?
easy
A. val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ }
B. val launcher = registerActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ }
C. val launcher = registerForResult(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ }
D. val launcher = registerActivity(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ }
Solution
Step 1: Identify the correct function name
The correct function is registerForActivityResult, not any shortened or altered form.
Step 2: Check the contract and lambda syntax
The contract ActivityResultContracts.StartActivityForResult() and lambda parameter { result -> } are correctly used in val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ }.
Final Answer:
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> /* handle result */ } -> Option A
Quick Check:
Correct function name and syntax = B [OK]
Hint: Look for exact function name: registerForActivityResult [OK]
Common Mistakes:
Using incorrect function names like registerActivityResult
Omitting parentheses in contract instantiation
Wrong lambda syntax
3. Given the following code snippet, what will be printed if the launched activity returns RESULT_OK with intent data containing key "username" = "Alice"?
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val name = result.data?.getStringExtra("username")
println(name)
} else {
println("No data")
}
}
medium
A. Crash due to null pointer
B. Alice
C. null
D. No data
Solution
Step 1: Check the result code condition
The code checks if result.resultCode == Activity.RESULT_OK, which is true in this case.
Step 2: Extract the "username" from intent data
The intent data contains key "username" with value "Alice", so name will be "Alice" and printed.
Final Answer:
Alice -> Option B
Quick Check:
Result OK with data returns value = D [OK]
Hint: Check resultCode and safely unwrap intent data [OK]
Common Mistakes:
Ignoring resultCode and printing 'No data'
Assuming data is null without checking
Not using safe call operator on intent data
4. Identify the error in this code snippet for handling activity results:
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val name = result.data!!.getStringExtra("username")
if (result.resultCode == Activity.RESULT_OK) {
println(name)
}
}
medium
A. Unsafe call on result.data before checking resultCode
B. Missing lambda parameter in registerForActivityResult
C. Incorrect contract used for activity result
D. No error, code is correct
Solution
Step 1: Analyze the order of operations
The code force unwraps result.data!! before checking if result.resultCode == Activity.RESULT_OK.
Step 2: Understand potential crash
If the result is not OK or data is null, force unwrapping causes a crash. The check should come before accessing data.
Final Answer:
Unsafe call on result.data before checking resultCode -> Option A
Quick Check:
Check resultCode before accessing data = A [OK]
Hint: Always check resultCode before using intent data [OK]
Common Mistakes:
Force unwrapping nullable data without checks
Accessing data before verifying resultCode
Ignoring null safety in Kotlin
5. You want to start an activity to pick a contact and get the contact's URI back. Which approach correctly uses registerForActivityResult to achieve this?
hard
A. val pickContact = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> if (granted) println("Permission granted") }
B. val pickContact = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> println(result.data?.getData().toString()) }
C. val pickContact = registerForActivityResult(ActivityResultContracts.PickContact()) { uri -> if (uri != null) println(uri.toString()) }
D. val pickContact = registerForActivityResult(ActivityResultContracts.TakePicture()) { success -> if (success) println("Picture taken") }
Solution
Step 1: Identify the correct contract for picking a contact
ActivityResultContracts.PickContact() is designed to pick a contact and return its URI.
Step 2: Check the lambda parameter and usage
The lambda receives a URI nullable parameter, which is safely checked before printing.
Step 3: Eliminate other options
val pickContact = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> println(result.data?.getData().toString()) } uses a generic contract but does not check resultCode; A and D are unrelated contracts for permissions and taking pictures.
Final Answer:
val pickContact = registerForActivityResult(ActivityResultContracts.PickContact()) { uri -> if (uri != null) println(uri.toString()) } -> Option C
Quick Check:
Use PickContact contract for contact URI = C [OK]
Hint: Use specific contracts like PickContact for contact picking [OK]
Common Mistakes:
Using StartActivityForResult without checking resultCode
Confusing permission or picture contracts with contact picking
Not handling nullable URI safely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.