What is the purpose of passing arguments between Android activities?
Passing arguments allows one activity to send data to another activity, enabling communication and data sharing between screens.
Click to reveal answer
beginner
How do you pass a simple string argument from one activity to another in Kotlin?
Use an Intent to start the new activity and add the string with putExtra(key, value). For example: intent.putExtra("username", "Alice").
Click to reveal answer
beginner
How do you retrieve a passed argument in the receiving activity?
In the receiving activity, use intent.getStringExtra(key) or the appropriate get method to get the data sent from the previous activity.
Click to reveal answer
intermediate
What is a Bundle in Android, and how is it related to passing arguments?
A Bundle is a container for key-value pairs used to pass data between activities or fragments. Intents use Bundles internally to carry extras.
Click to reveal answer
beginner
Why should you use constants for keys when passing arguments?
Using constants for keys avoids typos and makes code easier to maintain and understand, ensuring the same key is used when putting and getting data.
Click to reveal answer
Which method is used to add data to an Intent for passing arguments?
AgetExtra()
BstartActivity()
CputExtra()
DsetContentView()
✗ Incorrect
putExtra() adds data to the Intent to pass it to another activity.
How do you retrieve a string argument named "user" in the receiving activity?
AgetIntent().putExtra("user")
Bintent.putExtra("user")
CstartActivity(intent)
Dintent.getStringExtra("user")
✗ Incorrect
getStringExtra("user") retrieves the string passed with the key "user".
What is the role of a Bundle in passing arguments?
AIt stores key-value pairs of data to pass between components
BIt starts a new activity
CIt displays UI elements
DIt manages app permissions
✗ Incorrect
A Bundle holds data as key-value pairs to pass between activities or fragments.
Why is it recommended to use constants for keys when passing arguments?
ATo change UI colors
BTo avoid typos and keep keys consistent
CTo reduce app size
DTo increase app speed
✗ Incorrect
Constants prevent mistakes and ensure the same key is used everywhere.
Which of these is NOT a valid way to pass arguments in Android?
AUsing SharedPreferences for temporary data transfer
BUsing Bundles
CUsing Intent extras
DUsing startActivityForResult with Intent extras
✗ Incorrect
SharedPreferences is for persistent storage, not for passing temporary arguments between activities.
Explain how to pass a string argument from one Android activity to another using Kotlin.
Think about how you send data with an Intent and retrieve it in the new screen.
You got /5 concepts.
Describe why using constants for keys when passing arguments is a good practice.
Consider what happens if you mistype a key in one place.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of passing arguments between activities in Android Kotlin?
easy
A. To close the current activity
B. To change the app theme
C. To share data between different screens
D. To update the app version
Solution
Step 1: Understand activity communication
Activities often need to share information like user input or selections.
Step 2: Purpose of passing arguments
Passing arguments allows one screen to send data to another screen.
Final Answer:
To share data between different screens -> Option C
Quick Check:
Passing arguments = sharing data [OK]
Hint: Passing arguments means sending data between screens [OK]
Common Mistakes:
Thinking it changes app theme
Confusing with app version updates
Assuming it closes activities
2. Which Kotlin code correctly passes a String argument "username" with value "Alice" to another activity using Intent?
easy
A. val intent = Intent(this, NextActivity::class.java)
intent.putExtra("username", "Alice")
startActivity(intent)
B. val intent = Intent(this, NextActivity::class.java)
intent.putString("username", "Alice")
startActivity(intent)
C. val intent = Intent(this, NextActivity::class.java)
intent.addExtra("username", "Alice")
startActivity(intent)
D. val intent = Intent(this, NextActivity::class.java)
intent.setExtra("username", "Alice")
startActivity(intent)
Solution
Step 1: Identify correct Intent method
The method to add data is putExtra(key, value).
Step 2: Check method names in options
Only val intent = Intent(this, NextActivity::class.java)
intent.putExtra("username", "Alice")
startActivity(intent) uses putExtra correctly; others use invalid method names.
Final Answer:
val intent = Intent(this, NextActivity::class.java)\nintent.putExtra("username", "Alice")\nstartActivity(intent) -> Option A
Quick Check:
Use putExtra() to pass data [OK]
Hint: Use putExtra() to add data to Intent [OK]
Common Mistakes:
Using non-existent methods like putString or addExtra
Forgetting to call startActivity()
Passing wrong argument types
3. What will be the output of the following code in the receiving activity?
val username = intent.getStringExtra("username")
textView.text = username ?: "Guest"
If the sending activity did NOT pass any "username" extra?
medium
A. The app will crash with NullPointerException
B. The TextView will show "null"
C. The TextView will be empty
D. The TextView will show "Guest"
Solution
Step 1: Understand getStringExtra behavior
If no extra is passed, getStringExtra returns null.
Step 2: Analyze Elvis operator usage
The expression username ?: "Guest" means if username is null, use "Guest".
Final Answer:
The TextView will show "Guest" -> Option D
Quick Check:
Null fallback with ?: operator = "Guest" [OK]
Hint: ?: operator provides default if null [OK]
Common Mistakes:
Expecting null string to display
Assuming app crashes on null
Forgetting ?: fallback
4. Identify the error in this code snippet that tries to retrieve an Int extra named "age":
val age = intent.getIntExtra("age")
textView.text = age.toString()
medium
A. getIntExtra requires a default value as second argument
B. getIntExtra should be getIntExtras
C. age.toString() is invalid for Int
D. Intent cannot carry Int values
Solution
Step 1: Check getIntExtra method signature
getIntExtra requires two arguments: key and default value.
Step 2: Identify missing argument
The code misses the default value argument, causing a compile error.
Final Answer:
getIntExtra requires a default value as second argument -> Option A
Quick Check:
getIntExtra(key, default) needs default [OK]
Hint: Always provide default value for getIntExtra [OK]
Common Mistakes:
Omitting default value argument
Using wrong method name getIntExtras
Thinking Int can't be passed
5. You want to pass a list of Strings named "items" from one activity to another. Which is the correct way to pass and retrieve this list using Intent?
hard
A. Use intent.putExtra("items", items) and intent.getStringExtra("items")
B. Use intent.putStringArrayListExtra("items", items) and intent.getStringArrayListExtra("items")
C. Use intent.putExtra("items", items.toString()) and intent.getStringExtra("items")
D. Use intent.putExtra("items", items) and intent.getSerializableExtra("items")
Solution
Step 1: Passing a list of Strings
For ArrayList<String>, use putStringArrayListExtra to pass the list.
Step 2: Retrieving the list
Use getStringArrayListExtra to get the list back safely.
Final Answer:
Use intent.putStringArrayListExtra("items", items) and intent.getStringArrayListExtra("items") -> Option B
Quick Check:
Use put/getStringArrayListExtra for String lists [OK]
Hint: Use put/getStringArrayListExtra for String lists [OK]
Common Mistakes:
Passing list as plain putExtra
Converting list to string incorrectly
Using getSerializableExtra without casting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.