0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.