What if you could make your app screens talk to each other and share secrets instantly?
Why Passing arguments in Android Kotlin? - Purpose & Use Cases
Imagine you want to open a new screen in your app and show a personalized message. Without passing arguments, you would have to create a new screen for every possible message, which is like writing a new letter for every friend instead of just writing their name on a card.
Manually creating separate screens or hardcoding data for each case is slow and confusing. It leads to repeated code and makes your app hard to update or fix. It's like copying the same recipe many times with small changes instead of just changing the ingredients.
Passing arguments lets you send data from one screen to another easily. You write one screen and tell it what to show by giving it the right information. This saves time, reduces mistakes, and makes your app flexible and clean.
val intent = Intent(this, MessageActivity::class.java)
startActivity(intent) // No message passedval intent = Intent(this, MessageActivity::class.java) intent.putExtra("message", "Hello, friend!") startActivity(intent)
Passing arguments unlocks dynamic and personalized app experiences by sharing data smoothly between screens.
When you tap a contact in your phone app, it opens their details screen showing their name and number. This happens because the contact's info is passed as arguments to the details screen.
Passing arguments helps screens share data easily.
It avoids repeating code and makes apps flexible.
It enables personalized and dynamic user experiences.
Practice
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 CQuick Check:
Passing arguments = sharing data [OK]
- Thinking it changes app theme
- Confusing with app version updates
- Assuming it closes activities
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 AQuick Check:
Use putExtra() to pass data [OK]
- Using non-existent methods like putString or addExtra
- Forgetting to call startActivity()
- Passing wrong argument types
val username = intent.getStringExtra("username")
textView.text = username ?: "Guest"
If the sending activity did NOT pass any "username" extra?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 DQuick Check:
Null fallback with ?: operator = "Guest" [OK]
- Expecting null string to display
- Assuming app crashes on null
- Forgetting ?: fallback
val age = intent.getIntExtra("age")
textView.text = age.toString()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 AQuick Check:
getIntExtra(key, default) needs default [OK]
- Omitting default value argument
- Using wrong method name getIntExtras
- Thinking Int can't be passed
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 BQuick Check:
Use put/getStringArrayListExtra for String lists [OK]
- Passing list as plain putExtra
- Converting list to string incorrectly
- Using getSerializableExtra without casting
Ready to prove it?
Pass the test and this pattern moves unknown
