What if you could instantly share info between screens without messy workarounds?
Why Passing data between activities in Android Kotlin? - Purpose & Use Cases
Imagine you have two screens in your app: one where you enter your name, and another where you want to show a greeting using that name. Without a simple way to share data, you'd have to write extra code to save and find that name manually every time you switch screens.
Manually saving data between screens can be slow and confusing. You might forget to save the data, or accidentally show the wrong information. It's like trying to pass a note in class without a clear way to hand it over -- it can get lost or mixed up.
Passing data between activities lets you send information directly when you switch screens. It's like handing a note directly to your friend, so they get exactly what you want them to see, right when they need it.
Save data in a file or database, then read it again in the next screen.
val intent = Intent(this, NextActivity::class.java) intent.putExtra("name", "Alice") startActivity(intent)
This makes your app feel smooth and connected, letting screens share information instantly and easily.
When you log in, your username is passed to the next screen to personalize your experience without asking you again.
Passing data between activities helps screens share information directly.
It avoids extra work and mistakes from manual data saving.
Your app becomes more user-friendly and responsive.
Practice
Solution
Step 1: Understand Intent usage
Intent is the standard way to start a new activity and can carry data with putExtra().Step 2: Recognize data passing method
putExtra() attaches key-value pairs to the Intent for the next activity to retrieve.Final Answer:
Use an Intent with putExtra() to attach data. -> Option AQuick Check:
Intent + putExtra() = Data passing [OK]
- Trying to call methods of another activity directly
- Using global variables which is not recommended
- Assuming database is needed for simple data passing
Solution
Step 1: Recall Intent method for extras
putExtra() is the correct method to add data to an Intent.Step 2: Check syntax correctness
putExtra() takes a key and a value as parameters, matching intent.putExtra("username", username).Final Answer:
intent.putExtra("username", username) -> Option CQuick Check:
putExtra() syntax correct = intent.putExtra("username", username) [OK]
- Using non-existent methods like addExtra or setExtra
- Confusing putExtra with putData
- Missing quotes around the key string
val intent = Intent(this, ActivityB::class.java)
intent.putExtra("age", 25)
startActivity(intent)
What code correctly retrieves the "age" integer in Activity B?Solution
Step 1: Identify the data type sent
The value sent is an integer 25, so we must retrieve it as an Int.Step 2: Use correct retrieval method
getIntExtra() retrieves an Int extra with a default fallback value.Final Answer:
val age = intent.getIntExtra("age", 0) -> Option AQuick Check:
Integer extra retrieval = getIntExtra() [OK]
- Using getStringExtra() for an integer value
- Trying to cast getExtra() without safe methods
- Using non-existent getInt() method
val email = intent.getStringExtra("email")!!Solution
Step 1: Understand !! operator risk
Using !! forces a non-null value and crashes if the extra is missing (null).Step 2: Recognize safe retrieval practice
It's safer to check for null or provide a default instead of forcing !!.Final Answer:
Using !! can cause a crash if "email" extra is missing. -> Option BQuick Check:
Unsafe !! usage risks crash [OK]
- Assuming extras always exist without null checks
- Thinking getStringExtra() is invalid
- Confusing keys with resource IDs
Solution
Step 1: Identify correct method for List<Int>
Intent supports ArrayList<Int> via putIntegerArrayListExtra() and getIntegerArrayListExtra().Step 2: Confirm retrieval matches sending method
Using these methods ensures type safety and proper data transfer.Final Answer:
Use intent.putIntegerArrayListExtra("userIds", ArrayList(userIds)) and retrieve with getIntegerArrayListExtra("userIds") -> Option DQuick Check:
Use put/getIntegerArrayListExtra for List<Int> [OK]
- Trying to send List<Int> directly with putExtra()
- Using getIntExtra() for a list
- Using static variables which breaks encapsulation
Ready to prove it?
Pass the test and this pattern moves unknown
