Challenge - 5 Problems
Activity Result Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output when returning a result from an Activity?
Consider an Android app where Activity A starts Activity B for a result. Activity B sets the result as Intent with key "user_name" and value "Alice" before finishing. What will Activity A receive in onActivityResult?
Android Kotlin
val intent = Intent() intent.putExtra("user_name", "Alice") setResult(Activity.RESULT_OK, intent) finish()
Attempts:
2 left
💡 Hint
Remember that setResult passes data back to the calling Activity.
✗ Incorrect
When Activity B calls setResult with RESULT_OK and an Intent containing extras, Activity A receives that Intent in onActivityResult with the same extras.
❓ lifecycle
intermediate2:00remaining
What happens if you call finish() without setResult()?
In an Android app, Activity B is started for result by Activity A. Activity B calls finish() without calling setResult(). What result does Activity A receive?
Attempts:
2 left
💡 Hint
If setResult is not called, what is the default result code?
✗ Incorrect
If setResult is not called, the default result code is RESULT_CANCELED and no data is sent back.
📝 Syntax
advanced2:30remaining
Which code snippet correctly starts an Activity for result using the new Activity Result API?
Android introduced a new Activity Result API replacing startActivityForResult. Which snippet correctly registers and launches an Activity for result?
Android Kotlin
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
// handle the returned Uri
}
// To launch:
getContent.launch("image/*")Attempts:
2 left
💡 Hint
Look for the correct use of registerForActivityResult with a lambda receiving a result parameter.
✗ Incorrect
Option A correctly uses registerForActivityResult with StartActivityForResult contract and handles the result in the lambda.
🔧 Debug
advanced2:30remaining
Why does onActivityResult not get called after starting an Activity for result?
An app uses startActivityForResult to launch Activity B from Activity A. But onActivityResult in Activity A is never called after Activity B finishes. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check how Activity B is started.
✗ Incorrect
If Activity B is started with startActivity instead of startActivityForResult, onActivityResult will not be called.
🧠 Conceptual
expert3:00remaining
What is the advantage of the new Activity Result API over startActivityForResult?
Android introduced the Activity Result API to replace startActivityForResult and onActivityResult. Which is the main advantage of the new API?
Attempts:
2 left
💡 Hint
Think about lifecycle and code clarity improvements.
✗ Incorrect
The new Activity Result API uses lifecycle-aware components and callbacks, making result handling cleaner and avoiding deprecated methods.