Mudanças entre as edições de "Android Kotlin - Menus e Activities"

De Aulas
 
(7 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 4: Linha 4:
 
= Visual =
 
= Visual =
  
<center>Nesse exemplo vamos criar um projeto com um '''Basic Activity''', mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities.
+
Nesse exemplo vamos criar um projeto com um '''Basic Activity''', mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities.
[[Image:Android_studio_menus_activities.png|500px]]</center>
+
 
 +
Nosso exemplo pronto deve parecer com as imagens abaixo.
 +
 
 +
<center>[[Image:Android_studio_menus_activities.png|500px]]</center>
  
 
= strings.xml =
 
= strings.xml =
Linha 11: Linha 14:
 
<syntaxhighlight lang=xml line>
 
<syntaxhighlight lang=xml line>
 
<resources>
 
<resources>
     <string name="app_name">a001</string>
+
     <string name="app_name">Menu and Activity Test</string>
 
     <string name="action_settings">Settings</string>
 
     <string name="action_settings">Settings</string>
 
     <string name="action_other">Other</string>
 
     <string name="action_other">Other</string>
 
     <string name="name">Name</string>
 
     <string name="name">Name</string>
 
     <string name="age">Age</string>
 
     <string name="age">Age</string>
    <string name="result">Result</string>
 
 
     <string name="button_execute">Execute</string>
 
     <string name="button_execute">Execute</string>
     <string name="message">Your name is %1$s and you are %2$s years old.</string>
+
     <string name="message">Your name is %1$s and your age are %2$s years old.</string>
 
</resources>
 
</resources>
 
</syntaxhighlight>
 
</syntaxhighlight>
Linha 41: Linha 43:
 
== MainActivity.kt ==
 
== MainActivity.kt ==
  
Nos programa kotlin, basta adicionar as ações dentro da opção no wen.
+
Nos programa kotlin, basta adicionar as ações dentro da opção no <code>when</code>.
  
 
<syntaxhighlight lang=kotlin n>
 
<syntaxhighlight lang=kotlin n>
Linha 66: Linha 68:
 
<syntaxhighlight lang=xml n>
 
<syntaxhighlight lang=xml n>
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:android="http://schemas.android.com/apk/res/android"
+
    xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
+
    xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
+
    android:layout_width="match_parent"
        android:layout_width="match_parent"
+
    android:layout_height="match_parent"
        android:layout_height="match_parent"
+
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
 
        tools:showIn="@layout/activity_main"
 
        tools:context=".MainActivity">
 
  
    <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:text="Hello World!"
 
            app:layout_constraintLeft_toLeftOf="parent"
 
            app:layout_constraintRight_toRightOf="parent"
 
            app:layout_constraintTop_toTopOf="parent" android:id="@+id/textViewHello"
 
    />
 
 
     <EditText
 
     <EditText
            android:layout_width="0dp"
+
        android:id="@+id/editTextName"
            android:layout_height="wrap_content"
+
        android:layout_width="0dp"
            android:inputType="textPersonName"
+
        android:layout_height="wrap_content"
            android:ems="10"
+
        android:layout_marginTop="116dp"
            android:id="@+id/editTextNome"
+
        android:ems="10"
            tools:text="Nome" android:layout_marginTop="32dp" app:layout_constraintTop_toBottomOf="@+id/textViewHello"
+
        android:hint="@string/name"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
+
        android:inputType="textPersonName"
            android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
+
        app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginLeft="8dp" app:layout_constraintHorizontal_bias="0.0"/>
+
        app:layout_constraintHorizontal_bias="1.0"
 +
        app:layout_constraintStart_toStartOf="parent"
 +
        app:layout_constraintTop_toTopOf="parent" />
 +
 
 
     <EditText
 
     <EditText
            android:layout_width="0dp"
+
        android:id="@+id/editTextAge"
            android:layout_height="wrap_content"
+
        android:layout_width="0dp"
            android:inputType="textPersonName"
+
        android:layout_height="wrap_content"
            android:ems="10"
+
        android:ems="10"
            android:id="@+id/editTextIdade"
+
        android:hint="@string/age"
            tools:text="Idade" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/editTextNome"
+
        android:inputType="textPersonName"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
+
        app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
+
        app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"/>
+
        app:layout_constraintTop_toBottomOf="@+id/editTextName" />
  
</android.support.constraint.ConstraintLayout>
+
    <TextView
 +
        android:id="@+id/textViewMessage"
 +
        android:layout_width="0dp"
 +
        android:layout_height="wrap_content"
 +
        android:layout_marginTop="32dp"
 +
        android:textColor="#734BB9"
 +
        android:textScaleX="1.0"
 +
        android:textSize="20sp"
 +
        android:textStyle="normal"
 +
        app:layout_constraintEnd_toEndOf="parent"
 +
        app:layout_constraintHorizontal_bias="0.0"
 +
        app:layout_constraintStart_toStartOf="parent"
 +
        app:layout_constraintTop_toBottomOf="@+id/editTextAge" />
 +
</androidx.constraintlayout.widget.ConstraintLayout>
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== activitie_other.xml ==
+
== activity_other.xml ==
  
 
<syntaxhighlight lang=xml n>
 
<syntaxhighlight lang=xml n>
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:android="http://schemas.android.com/apk/res/android"
+
    xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
+
    xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
+
    android:layout_width="match_parent"
        android:layout_width="match_parent"
+
    android:layout_height="match_parent"
        android:layout_height="match_parent"
+
    tools:context=".OtherActivity">
        tools:context=".OtherActivity">
+
 
 +
    <TextView
 +
        android:id="@+id/textViewName"
 +
        android:layout_width="0dp"
 +
        android:layout_height="wrap_content"
 +
        android:layout_marginTop="36dp"
 +
        android:text="@string/name"
 +
        app:layout_constraintEnd_toEndOf="parent"
 +
        app:layout_constraintHorizontal_bias="0.0"
 +
        app:layout_constraintStart_toStartOf="parent"
 +
        app:layout_constraintTop_toTopOf="parent" />
  
 
     <TextView
 
     <TextView
            android:text="Oi, sou a outra Activity"
+
        android:id="@+id/textViewAge"
            android:layout_width="wrap_content"
+
        android:layout_width="0dp"
            android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
            android:id="@+id/textView"
+
        android:layout_marginTop="24dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" android:layout_marginTop="84dp"
+
        android:text="@string/age"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="8dp"
+
        app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginEnd="8dp"
+
        app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
+
        app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.497"/>
+
        app:layout_constraintTop_toBottomOf="@+id/textViewName" />
 +
 
 
     <Button
 
     <Button
            android:text="Voltar"
+
        android:id="@+id/buttonExecute"
            android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
            android:id="@+id/buttonVoltar" android:layout_marginTop="92dp"
+
        android:layout_marginTop="88dp"
            app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintStart_toStartOf="parent"
+
        android:text="@string/button_execute"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
+
        app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
+
        app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintHorizontal_bias="0.498"/>
+
        app:layout_constraintStart_toStartOf="parent"
    <TextView
+
        app:layout_constraintTop_toBottomOf="@+id/textViewAge" />
            android:text="NOME"
+
</androidx.constraintlayout.widget.ConstraintLayout>
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:id="@+id/textViewNome" android:layout_marginTop="116dp"
 
            app:layout_constraintTop_toBottomOf="@+id/buttonVoltar" app:layout_constraintStart_toStartOf="parent"
 
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
 
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
 
            app:layout_constraintHorizontal_bias="0.498"
 
            android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
 
    <TextView
 
            android:text="IDADE"
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:id="@+id/textViewIdade" android:layout_marginTop="104dp"
 
            app:layout_constraintTop_toBottomOf="@+id/textViewNome" app:layout_constraintEnd_toEndOf="parent"
 
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
 
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
 
            android:layout_marginStart="8dp" android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
 
</android.support.constraint.ConstraintLayout>
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Linha 166: Linha 166:
  
 
<syntaxhighlight lang=kotlin n>
 
<syntaxhighlight lang=kotlin n>
package com.arisa.exemplo
+
package com.example.menuteste
  
 
import android.app.Activity
 
import android.app.Activity
Linha 173: Linha 173:
 
import com.google.android.material.snackbar.Snackbar
 
import com.google.android.material.snackbar.Snackbar
 
import androidx.appcompat.app.AppCompatActivity
 
import androidx.appcompat.app.AppCompatActivity
 +
import androidx.navigation.ui.AppBarConfiguration
 
import android.view.Menu
 
import android.view.Menu
 
import android.view.MenuItem
 
import android.view.MenuItem
import android.widget.ActionMenuView
 
 
import android.widget.EditText
 
import android.widget.EditText
 
import android.widget.TextView
 
import android.widget.TextView
 +
import androidx.activity.result.contract.ActivityResultContracts
 +
import com.example.menuteste.databinding.ActivityMainBinding
  
import kotlinx.android.synthetic.main.activity_main.*
+
class MainActivity : AppCompatActivity() {
  
class MainActivity : AppCompatActivity() {
+
    private lateinit var appBarConfiguration: AppBarConfiguration
 +
    private lateinit var binding: ActivityMainBinding
  
 
     override fun onCreate(savedInstanceState: Bundle?) {
 
     override fun onCreate(savedInstanceState: Bundle?) {
 
         super.onCreate(savedInstanceState)
 
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
+
        binding = ActivityMainBinding.inflate(layoutInflater)
         setSupportActionBar(toolbar)
+
         setContentView(binding.root)
 +
         setSupportActionBar(binding.toolbar)
  
         fab.setOnClickListener { view ->
+
         binding.fab.setOnClickListener { view ->
             //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
+
             Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            //        .setAction("Action", null).show()
+
                .setAction("Action", null).show()
            abreOutraJanela()
 
 
         }
 
         }
 
     }
 
     }
Linha 200: Linha 203:
 
     }
 
     }
  
     fun abreOutraJanela() {
+
     private fun openOtherActivity() {
 
         val editTextName : EditText = findViewById(R.id.editTextName)
 
         val editTextName : EditText = findViewById(R.id.editTextName)
 
         val editTextAge : EditText = findViewById(R.id.editTextAge)
 
         val editTextAge : EditText = findViewById(R.id.editTextAge)
Linha 207: Linha 210:
 
         intent.putExtra("name", editTextName.text.toString())
 
         intent.putExtra("name", editTextName.text.toString())
 
         intent.putExtra("age", editTextAge.text.toString())
 
         intent.putExtra("age", editTextAge.text.toString())
         startActivityForResult(intent, 1)
+
         getResult.launch(intent)
 
     }
 
     }
  
Linha 214: Linha 217:
 
             R.id.action_settings -> true
 
             R.id.action_settings -> true
 
             R.id.action_other -> {
 
             R.id.action_other -> {
                 abreOutraJanela()
+
                 openOtherActivity()
 
                 true
 
                 true
 
             }
 
             }
Linha 221: Linha 224:
 
     }
 
     }
  
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+
     private val getResult =
        super.onActivityResult(requestCode, resultCode, data)
+
        registerForActivityResult(
         if (resultCode == Activity.RESULT_OK) {
+
            ActivityResultContracts.StartActivityForResult()
            if (data != null) {
+
         ) {
                val textViewResult : TextView = findViewById(R.id.textViewResult)
+
            if (it.resultCode == Activity.RESULT_OK) {
                textViewResult.text = data.getStringExtra("result")
+
                if (it.data != null) {
 +
                    val textViewMessage : TextView = findViewById(R.id.textViewMessage)
 +
                    textViewMessage.text = it.data?.getStringExtra("message")
 +
                }
 
             }
 
             }
 
         }
 
         }
 
    }
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Linha 237: Linha 241:
  
 
<syntaxhighlight lang=kotlin n>
 
<syntaxhighlight lang=kotlin n>
package com.arisa.exemplo
+
package com.example.menuteste
  
 
import android.content.Intent
 
import android.content.Intent
Linha 246: Linha 250:
  
 
class OtherActivity : AppCompatActivity() {
 
class OtherActivity : AppCompatActivity() {
 
 
     override fun onCreate(savedInstanceState: Bundle?) {
 
     override fun onCreate(savedInstanceState: Bundle?) {
 
         super.onCreate(savedInstanceState)
 
         super.onCreate(savedInstanceState)
 
         setContentView(R.layout.activity_other)
 
         setContentView(R.layout.activity_other)
  
         val textViewName : TextView = findViewById(R.id.textViewName)
+
         val textViewName: TextView = findViewById(R.id.textViewName)
 
         val textViewAge : TextView = findViewById(R.id.textViewAge)
 
         val textViewAge : TextView = findViewById(R.id.textViewAge)
  
Linha 260: Linha 263:
 
         }
 
         }
  
         val buttonExecute : Button = findViewById(R.id.buttonExecute)
+
         val buttonExecute: Button = findViewById(R.id.buttonExecute)
 
         buttonExecute.setOnClickListener {
 
         buttonExecute.setOnClickListener {
             val out = getString(R.string.message, textViewName.text, textViewAge.text)
+
             val message = getString(R.string.message, textViewName.text, textViewAge.text)
 
             val data = Intent()
 
             val data = Intent()
             data.putExtra("result", out)
+
             data.putExtra("message", message)
 
             setResult(RESULT_OK, data)
 
             setResult(RESULT_OK, data)
 
             finish()
 
             finish()

Edição atual tal como às 20h48min de 3 de maio de 2023

Afluentes: Dispositivos Móveis

Visual

Nesse exemplo vamos criar um projeto com um Basic Activity, mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities.

Nosso exemplo pronto deve parecer com as imagens abaixo.

Android studio menus activities.png

strings.xml

1<resources>
2    <string name="app_name">Menu and Activity Test</string>
3    <string name="action_settings">Settings</string>
4    <string name="action_other">Other</string>
5    <string name="name">Name</string>
6    <string name="age">Age</string>
7    <string name="button_execute">Execute</string>
8    <string name="message">Your name is %1$s and your age are %2$s years old.</string>
9</resources>

Menus

menu_main.xml

No menu_main.xml, adiciona-se os novos itens. A informação orderInCategory é a ordem que os itens do meno irão aparecer.

    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:orderInCategory="100"
          app:showAsAction="never"/>
    <item android:id="@+id/action_other"
          android:title="@string/action_other"
          android:orderInCategory="101"
          app:showAsAction="never"/>

MainActivity.kt

Nos programa kotlin, basta adicionar as ações dentro da opção no when.

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val myText: TextView = findViewById(R.id.textViewHello)
        return when (item.itemId) {
            R.id.action_settings -> {
                myText.text = "Settings option"
                return true
            }
            R.id.action_other -> {
                myText.text = "Other option"
                return true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

Layouts

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:ems="10"
        android:hint="@string/name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextAge"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/age"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextName" />

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:textColor="#734BB9"
        android:textScaleX="1.0"
        android:textSize="20sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextAge" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_other.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OtherActivity">

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:text="@string/name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textViewAge"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/age"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewName" />

    <Button
        android:id="@+id/buttonExecute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="@string/button_execute"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewAge" />
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin

MainActivity.kt

package com.example.menuteste

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.ui.AppBarConfiguration
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import com.example.menuteste.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)

        binding.fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    private fun openOtherActivity() {
        val editTextName : EditText = findViewById(R.id.editTextName)
        val editTextAge : EditText = findViewById(R.id.editTextAge)

        val intent = Intent(this, OtherActivity::class.java)
        intent.putExtra("name", editTextName.text.toString())
        intent.putExtra("age", editTextAge.text.toString())
        getResult.launch(intent)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_settings -> true
            R.id.action_other -> {
                openOtherActivity()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    private val getResult =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                if (it.data != null) {
                    val textViewMessage : TextView = findViewById(R.id.textViewMessage)
                    textViewMessage.text = it.data?.getStringExtra("message")
                }
            }
        }
}

OtherActivity.kt

package com.example.menuteste

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

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

        val textViewName: TextView = findViewById(R.id.textViewName)
        val textViewAge : TextView = findViewById(R.id.textViewAge)

        val bundle = intent.extras
        if (bundle != null) {
            textViewName.text = bundle.getString("name")
            textViewAge.text = bundle.getString("age")
        }

        val buttonExecute: Button = findViewById(R.id.buttonExecute)
        buttonExecute.setOnClickListener {
            val message = getString(R.string.message, textViewName.text, textViewAge.text)
            val data = Intent()
            data.putExtra("message", message)
            setResult(RESULT_OK, data)
            finish()
        }
    }
}