0
0
Android Kotlinmobile~20 mins

Activity results in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Activity Result Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()
AActivity A receives RESULT_OK and intent with "user_name" = "Alice"
BActivity A receives RESULT_CANCELED and no data
CActivity A receives RESULT_OK but intent is null
DActivity A crashes due to missing data
Attempts:
2 left
💡 Hint
Remember that setResult passes data back to the calling Activity.
lifecycle
intermediate
2: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?
AActivity A receives null Intent and crashes
BActivity A receives RESULT_OK with no data
CActivity A receives RESULT_OK with default data
DActivity A receives RESULT_CANCELED with no data
Attempts:
2 left
💡 Hint
If setResult is not called, what is the default result code?
📝 Syntax
advanced
2: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/*")
A
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val data = result.data } }
launcher.launch(intent)
B
startActivityForResult(intent, REQUEST_CODE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { }
Cval launcher = startActivityForResult(intent, REQUEST_CODE)
D
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> /* no parameters */ }
launcher.launch(intent)
Attempts:
2 left
💡 Hint
Look for the correct use of registerForActivityResult with a lambda receiving a result parameter.
🔧 Debug
advanced
2: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?
AActivity B calls finish() without setResult()
BActivity B is launched with startActivity instead of startActivityForResult
CActivity A overrides onActivityResult but forgets to call super.onActivityResult
DActivity A uses the new Activity Result API but also overrides onActivityResult
Attempts:
2 left
💡 Hint
Check how Activity B is started.
🧠 Conceptual
expert
3: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?
AIt allows multiple Activities to run simultaneously in the background
BIt automatically saves and restores Activity state without extra code
CIt simplifies result handling by using lifecycle-aware callbacks and avoids deprecated methods
DIt removes the need to create Intents for starting Activities
Attempts:
2 left
💡 Hint
Think about lifecycle and code clarity improvements.