Bird
Raised Fist0
Android Kotlinmobile~20 mins

Passing data between activities in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: MessageSender
This screen lets the user enter a message and send it to another screen that displays it.
Target UI
-------------------------
| Enter your message:   |
| [______________]      |
|                       |
| [Send Message Button] |
-------------------------
Add an EditText for user to type a message.
Add a Button labeled 'Send Message'.
When the button is clicked, open a new activity called MessageReceiver.
Pass the typed message to MessageReceiver using Intent extras.
In MessageReceiver, display the received message in a TextView.
Starter Code
Android Kotlin
package com.example.passingdata

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MessageSender : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message_sender)

        val editTextMessage = findViewById<EditText>(R.id.editTextMessage)
        val buttonSend = findViewById<Button>(R.id.buttonSend)

        // TODO: Add button click listener to send message to MessageReceiver
    }
}

// TODO: Create MessageReceiver activity that receives and shows the message
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Solution
Android Kotlin
package com.example.passingdata

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MessageSender : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message_sender)

        val editTextMessage = findViewById<EditText>(R.id.editTextMessage)
        val buttonSend = findViewById<Button>(R.id.buttonSend)

        buttonSend.setOnClickListener {
            val message = editTextMessage.text.toString()
            val intent = Intent(this, MessageReceiver::class.java)
            intent.putExtra("EXTRA_MESSAGE", message)
            startActivity(intent)
        }
    }
}

class MessageReceiver : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message_receiver)

        val textViewReceived = findViewById<TextView>(R.id.textViewReceived)
        val message = intent.getStringExtra("EXTRA_MESSAGE") ?: "No message received"
        textViewReceived.text = message
    }
}

We added a click listener to the Send button in MessageSender. When clicked, it reads the text from the EditText, creates an Intent to start MessageReceiver, and puts the message as an extra with key "EXTRA_MESSAGE". Then it starts the new activity.

In MessageReceiver, we get the Intent that started it and read the string extra with the same key. We show this message in a TextView. This way, data passes from one screen to another simply and clearly.

Final Result
Completed Screen
-------------------------
| Enter your message:   |
| [Hello World!]        |
|                       |
| [Send Message Button] |
-------------------------

-- After tapping Send --
-------------------------
| Message received:     |
| Hello World!          |
-------------------------
User types a message in the EditText.
User taps 'Send Message' button.
App opens MessageReceiver screen showing the typed message.
Stretch Goal
Add a Toast message in MessageSender confirming the message was sent.
💡 Hint
Use Toast.makeText(context, "Message sent", Toast.LENGTH_SHORT).show() inside the button click listener.

Practice

(1/5)
1. What is the primary method to send data from one activity to another in Android Kotlin?
easy
A. Use an Intent with putExtra() to attach data.
B. Use a global variable to share data.
C. Call the other activity's method directly.
D. Use a database to pass data instantly.

Solution

  1. Step 1: Understand Intent usage

    Intent is the standard way to start a new activity and can carry data with putExtra().
  2. Step 2: Recognize data passing method

    putExtra() attaches key-value pairs to the Intent for the next activity to retrieve.
  3. Final Answer:

    Use an Intent with putExtra() to attach data. -> Option A
  4. Quick Check:

    Intent + putExtra() = Data passing [OK]
Hint: Remember: Intent carries data with putExtra() [OK]
Common Mistakes:
  • Trying to call methods of another activity directly
  • Using global variables which is not recommended
  • Assuming database is needed for simple data passing
2. Which of the following is the correct syntax to add a String extra named "username" to an Intent in Kotlin?
easy
A. intent.setExtra("username", username)
B. intent.addExtra("username" to username)
C. intent.putExtra("username", username)
D. intent.putData("username", username)

Solution

  1. Step 1: Recall Intent method for extras

    putExtra() is the correct method to add data to an Intent.
  2. Step 2: Check syntax correctness

    putExtra() takes a key and a value as parameters, matching intent.putExtra("username", username).
  3. Final Answer:

    intent.putExtra("username", username) -> Option C
  4. Quick Check:

    putExtra() syntax correct = intent.putExtra("username", username) [OK]
Hint: Use putExtra(key, value) to add data to Intent [OK]
Common Mistakes:
  • Using non-existent methods like addExtra or setExtra
  • Confusing putExtra with putData
  • Missing quotes around the key string
3. Given this code in Activity A:
val intent = Intent(this, ActivityB::class.java)
intent.putExtra("age", 25)
startActivity(intent)
What code correctly retrieves the "age" integer in Activity B?
medium
A. val age = intent.getIntExtra("age", 0)
B. val age = intent.getStringExtra("age")
C. val age = intent.getExtra("age") as Int
D. val age = intent.getInt("age")

Solution

  1. Step 1: Identify the data type sent

    The value sent is an integer 25, so we must retrieve it as an Int.
  2. Step 2: Use correct retrieval method

    getIntExtra() retrieves an Int extra with a default fallback value.
  3. Final Answer:

    val age = intent.getIntExtra("age", 0) -> Option A
  4. Quick Check:

    Integer extra retrieval = getIntExtra() [OK]
Hint: Use get<Type>Extra(key, default) matching sent data type [OK]
Common Mistakes:
  • Using getStringExtra() for an integer value
  • Trying to cast getExtra() without safe methods
  • Using non-existent getInt() method
4. What is wrong with this code snippet in Activity B for retrieving a String extra named "email"?
val email = intent.getStringExtra("email")!!
medium
A. getStringExtra() does not exist for Intent.
B. Using !! can cause a crash if "email" extra is missing.
C. The key "email" should be an integer resource ID.
D. Intent extras cannot hold String values.

Solution

  1. Step 1: Understand !! operator risk

    Using !! forces a non-null value and crashes if the extra is missing (null).
  2. Step 2: Recognize safe retrieval practice

    It's safer to check for null or provide a default instead of forcing !!.
  3. Final Answer:

    Using !! can cause a crash if "email" extra is missing. -> Option B
  4. Quick Check:

    Unsafe !! usage risks crash [OK]
Hint: Avoid !! on extras; check for null safely [OK]
Common Mistakes:
  • Assuming extras always exist without null checks
  • Thinking getStringExtra() is invalid
  • Confusing keys with resource IDs
5. You want to pass a list of user IDs (List<Int>) from Activity A to Activity B. Which approach correctly sends and retrieves this list?
hard
A. Store the list in a static variable and access it directly in Activity B
B. Use intent.putExtra("userIds", userIds) and retrieve with getIntExtra("userIds", 0)
C. Serialize the list to JSON string and send with putExtra("userIds", jsonString), then parse in Activity B
D. Use intent.putIntegerArrayListExtra("userIds", ArrayList(userIds)) and retrieve with getIntegerArrayListExtra("userIds")

Solution

  1. Step 1: Identify correct method for List<Int>

    Intent supports ArrayList<Int> via putIntegerArrayListExtra() and getIntegerArrayListExtra().
  2. Step 2: Confirm retrieval matches sending method

    Using these methods ensures type safety and proper data transfer.
  3. Final Answer:

    Use intent.putIntegerArrayListExtra("userIds", ArrayList(userIds)) and retrieve with getIntegerArrayListExtra("userIds") -> Option D
  4. Quick Check:

    Use put/getIntegerArrayListExtra for List<Int> [OK]
Hint: Use put/getIntegerArrayListExtra for List<Int> [OK]
Common Mistakes:
  • Trying to send List<Int> directly with putExtra()
  • Using getIntExtra() for a list
  • Using static variables which breaks encapsulation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard