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] | -------------------------
------------------------- | Enter your message: | | [______________] | | | | [Send Message Button] | -------------------------
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
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.
------------------------- | Enter your message: | | [Hello World!] | | | | [Send Message Button] | ------------------------- -- After tapping Send -- ------------------------- | Message received: | | Hello World! | -------------------------
val intent = Intent(this, ActivityB::class.java)
intent.putExtra("age", 25)
startActivity(intent)
What code correctly retrieves the "age" integer in Activity B?val email = intent.getStringExtra("email")!!Pass the test and this pattern moves unknown