0
0
Android Kotlinmobile~20 mins

Activity results in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: MainActivity
This screen lets the user open a second screen to enter their name. When the user returns, the main screen shows the entered name.
Target UI
-------------------------
|       Main Screen      |
|                       |
|  Name: [___________]   |
|                       |
|  [Enter Name Button]   |
-------------------------
Add a button labeled 'Enter Name' on the main screen.
When tapped, open a second screen to enter a name.
The second screen has a TextField and a Save button.
When Save is tapped, return the entered name to the main screen.
Display the returned name on the main screen below the label 'Name:'.
Starter Code
Android Kotlin
package com.example.activityresults

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var nameTextView: TextView
    private lateinit var enterNameButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        nameTextView = findViewById(R.id.nameTextView)
        enterNameButton = findViewById(R.id.enterNameButton)

        // TODO: Register activity result launcher and set button click listener
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.activityresults

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var nameTextView: TextView
    private lateinit var enterNameButton: Button
    private lateinit var getNameLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        nameTextView = findViewById(R.id.nameTextView)
        enterNameButton = findViewById(R.id.enterNameButton)

        getNameLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
                val name = result.data?.getStringExtra("name") ?: ""
                nameTextView.text = "Name: $name"
            }
        }

        enterNameButton.setOnClickListener {
            val intent = Intent(this, EnterNameActivity::class.java)
            getNameLauncher.launch(intent)
        }
    }
}

// Second activity to enter name
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class EnterNameActivity : AppCompatActivity() {
    private lateinit var nameEditText: EditText
    private lateinit var saveButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_enter_name)

        nameEditText = findViewById(R.id.nameEditText)
        saveButton = findViewById(R.id.saveButton)

        saveButton.setOnClickListener {
            val name = nameEditText.text.toString()
            val resultIntent = intent
            resultIntent.putExtra("name", name)
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }
    }
}

We use registerForActivityResult with StartActivityForResult to launch the second screen and get the result back.

When the user taps the 'Enter Name' button, we start EnterNameActivity.

In EnterNameActivity, the user types a name and taps Save. We put the name into the result intent and finish the activity.

Back in MainActivity, the launcher callback receives the result and updates the TextView to show the entered name.

Final Result
Completed Screen
-------------------------
|       Main Screen      |
|                       |
|  Name: Alice           |
|                       |
|  [Enter Name Button]   |
-------------------------
User taps 'Enter Name Button'.
Second screen opens with a text field and Save button.
User types 'Alice' and taps Save.
Second screen closes and main screen shows 'Name: Alice'.
Stretch Goal
Add a Cancel button on the second screen that closes it without returning a name.
💡 Hint
Set result as RESULT_CANCELED and call finish() when Cancel is tapped.