Why does the reply send after two button clicks from the Second Activity to the Main Activity instead of one?

28 views Asked by At

This is the code for the activity_main.xml and MainActivity.kt:

<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=".MainActivity">

    <TextView
        android:id="@+id/reply_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/reply_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reply_header" />

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <EditText
        android:id="@+id/editText_send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:hint="Type Message Here!"
        android:inputType="textLongMessage"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button_send"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

package com.example.twoactivities

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

class MainActivity : AppCompatActivity() {

    private val secondActivityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->

        if (result.resultCode == RESULT_OK) {
            val data: Intent? = result.data
            val replyMessage = data?.getStringExtra("REPLY_KEY_VALUE")

            if (replyMessage != null) {
                val headerTextView: TextView = findViewById(R.id.reply_header)
                val textView: TextView = findViewById(R.id.reply_message)
                textView.text = replyMessage
                headerTextView.text = "Reply Received"
            }
        }
    }

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

        //val replyMessage = intent.getStringExtra("REPLY_KEY_VALUE")
        //val replyHeader = intent.getStringExtra("REPLY_HEADER_VALUE")

        //val replyHeaderTextView: TextView = findViewById(R.id.reply_header)
        //val replyMessageTextView: TextView = findViewById(R.id.reply_message)

        //replyHeaderTextView.text = replyHeader
        //replyMessageTextView.text = replyMessage

        val mMessageEditText: EditText = findViewById(R.id.editText_send)
        val buttonSend: Button = findViewById(R.id.button_send)

        buttonSend.setOnClickListener {

            val secondActivityIntent = Intent(this, SecondActivity::class.java)

            val message = mMessageEditText.text.toString().trim()

            secondActivityIntent.putExtra("SEND_KEY_VALUE", message)

            startActivity(secondActivityIntent)

            secondActivityLauncher.launch(secondActivityIntent)
        }
    }
}

This is the code for the activity_second.xml and SecondActivity.kt

<?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=".SecondActivity">

    <TextView
        android:id="@+id/text_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:text="Message Received"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_header" />

    <Button
        android:id="@+id/button_reply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <EditText
        android:id="@+id/editText_reply"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:hint="Enter Your Reply Here"
        android:inputType="textLongMessage"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button_reply"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.twoactivities

import android.app.Activity
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 SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_second)

        val message = intent.getStringExtra("SEND_KEY_VALUE")

        val textView: TextView = findViewById(R.id.text_message)

        textView.text = message

        val rReplyEditText: EditText = findViewById(R.id.editText_reply)
        val buttonReply: Button = findViewById(R.id.button_reply)

        buttonReply.setOnClickListener {

            val mainActivityIntent = Intent(this, MainActivity::class.java)

            //val replyHeader = "Reply Received"
            val replyMessage = rReplyEditText.text.toString().trim()

            //mainActivityIntent.putExtra("REPLY_HEADER_VALUE", replyHeader)
            mainActivityIntent.putExtra("REPLY_KEY_VALUE", replyMessage)

            setResult(RESULT_OK, mainActivityIntent)
            finish()

        }
    }
}

I have written some code here using the registerForActivityResult(ActivityResultContracts.StartActivityForResult()) method to send a reply from the Second Activity back to the Main Activity but when I click the "Send" button on Second Activity it doesn't send the reply I have typed out but when I click the button again regardless of whether I have typed nothing or a new reply it takes me to Main Activity and shows me the first message I typed. What could be the cause of this?

1

There are 1 answers

0
Hezy Ziv On BEST ANSWER

you're launching the SecondActivity twice in your MainActivity:

startActivity(secondActivityIntent)            // This starts the SecondActivity for the first time.
secondActivityLauncher.launch(secondActivityIntent)  // This starts the SecondActivity for the second time.

remove the first one