Challenge - 5 Problems
Argument Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediatePassing arguments between Android fragments
You want to pass a string argument from FragmentA to FragmentB using a Bundle. Which code snippet correctly sets the argument in FragmentA before navigation?
Android Kotlin
val bundle = Bundle() bundle.putString("userName", "Alice") val fragmentB = FragmentB() fragmentB.arguments = bundle // Navigate to fragmentB
Attempts:
2 left
💡 Hint
Remember to use putString with both key and value, and assign the bundle to the fragment's arguments.
✗ Incorrect
Option D correctly creates a Bundle, puts a string with key "userName", assigns it to fragmentB.arguments. Option D uses putInt with a string key which is incorrect for a string value. Option D misses the value in putString causing a syntax error. Option D tries to call putString on a null arguments property before initializing it.
📝 Syntax
intermediateCorrect syntax for passing arguments in Kotlin function
Which option shows the correct way to define a Kotlin function that takes a string argument and prints it?
Android Kotlin
fun greet(name: String) {
println("Hello, $name")
}Attempts:
2 left
💡 Hint
In Kotlin, parameter types come after the name separated by a colon.
✗ Incorrect
Option B uses correct Kotlin syntax: parameter name followed by colon and type. Option B misses the type causing a syntax error. Option B uses incorrect arrow syntax for function body. Option B uses Java-like syntax which is invalid in Kotlin.
❓ lifecycle
advancedAccessing passed arguments in Fragment lifecycle
Where is the best place to safely retrieve arguments passed to a Fragment in Android?
Attempts:
2 left
💡 Hint
Arguments are available early in the fragment lifecycle but before view creation.
✗ Incorrect
Option A is correct because onCreate() is called early and requireArguments() safely returns the Bundle or throws if missing. Option A risks null pointer without null check. Option A is too early and unsafe to use !! operator. Option A is too late as view is destroyed.
advanced
Passing arguments using Safe Args in Navigation Component
Using Android Navigation Component with Safe Args, how do you pass an integer argument named userId from FragmentA to FragmentB?
Attempts:
2 left
💡 Hint
Safe Args generates directions classes with methods to pass arguments safely.
✗ Incorrect
Option C uses the generated directions class from FragmentA to FragmentB with the argument userId. Option C and C use manual bundles which are valid but not Safe Args. Option C reverses the direction from FragmentB to FragmentA which is incorrect here.
🔧 Debug
expertWhy does this argument passing code crash?
Given this code in FragmentB, why does the app crash with a NullPointerException?
val userName = arguments?.getString("userName")!!
textView.text = userName
Attempts:
2 left
💡 Hint
Check if arguments Bundle is actually set before accessing it with !!
✗ Incorrect
The code uses safe call ?. on arguments but then forces non-null with !!. If arguments is null, this causes a NullPointerException. Option A is wrong because empty string is valid. Option A could cause crash but question focuses on argument passing. Option A is wrong because getString returns String or null.
