Clicking on button in Activity B does not go inside setOnClickListener and navigate to Activity A

25 views Asked by At

I have a notes app I am creating. I created 2 activities and have button that on click should save notes and navigate to MainActivity and display notes.

binding.btnSaveNotes.setOnClickListener {
        Toast.makeText(this@AddNotes, "Check Mark Clicked", Toast.LENGTH_SHORT).show()

        val title = binding.editTextTitle.text.toString()
        val noteDesc = binding.editTextDesc.text.toString()
        
        if (title.isNotEmpty() || noteDesc.isNotEmpty()) {
            val formatter = SimpleDateFormat("EEE, d MMM yyy HH:mm a")

            note = if (isUpdate) {
                Note(old_note.id, title, noteDesc, formatter.format(Date()))
            } else {
                Note(null, title, noteDesc, formatter.format(Date()))
            }

            val intent = Intent()
            intent.putExtra("note", note)
            setResult(Activity.RESULT_OK, intent)
            finish()

        } else {
            Toast.makeText(this@AddNotes, "Please enter some data", Toast.LENGTH_SHORT).show()
            return@setOnClickListener
        }
    }

I have tried updating notes app to have button/floating action button or image image. Added back arrow as well. It does not seem to go inside this code post button click.

1

There are 1 answers

0
Sarah On

The code you uploaded is not enough, so I will make the related code I practiced before similar to your question.

Create the MainActivity layout (activity_main.xml) with a button to navigate to the AddNotes activity:

<!-- activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnAddNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Add Note" />
</RelativeLayout>

Create the MainActivity class (MainActivity.kt):

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

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

        btnAddNote.setOnClickListener {
            val intent = Intent(this, AddNotes::class.java)
            startActivityForResult(intent, 1)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            val note = data?.getSerializableExtra("note") as Note
            // Handle the returned note data here and display it in the MainActivity UI.
        }
    }
}

Create the AddNotes layout (activity_add_notes.xml) with an EditText for title, an EditText for description, and a button to save the note:

<!-- activity_add_notes.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editTextTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Title" />

    <EditText
        android:id="@+id/editTextDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Description" />

    <Button
        android:id="@+id/btnSaveNotes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />
</LinearLayout>

Create the AddNotes class (AddNotes.kt):

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_add_notes.*
import java.text.SimpleDateFormat
import java.util.*

class AddNotes : AppCompatActivity() {

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

        btnSaveNotes.setOnClickListener {
            val title = editTextTitle.text.toString()
            val noteDesc = editTextDesc.text.toString()
            if (title.isNotEmpty() || noteDesc.isNotEmpty()) {
                val formatter = SimpleDateFormat("EEE, d MMM yyy HH:mm a")
                val note = Note(null, title, noteDesc, formatter.format(Date()))

                val intent = Intent()
                intent.putExtra("note", note)
                setResult(Activity.RESULT_OK, intent)
                finish()
            } else {
                // Display an error message or handle empty fields.
            }
        }
    }
}

Create a data class Note to hold your note information:

import java.io.Serializable

data class Note(
    val id: Long?,
    val title: String,
    val description: String,
    val timestamp: String
) : Serializable

I tested it and this code will work... Good luck.