Search code examples
androidkotlinandroid-activity

Cannot get onActivityResult() to be called, no matter what


Im making a simple note taking app in Android Studio, and im passing a Note object via Bundle to my 2nd Activity, and everything works. However, the changes made to that Note object is not reflected on the original object, as I assume when its parceled it creates a new reference (I'm still new to Android Studio, so I'm not certain).



    override fun onItemClick(note: Note) {
        val intent = Intent(this@MainActivity, NoteViewActivity::class.java)
        val bundle = Bundle()
        bundle.putParcelable("note", note)
        intent.putExtras(bundle)

//        val startForResult = registerForActivityResult(
//            ActivityResultContracts.StartActivityForResult()
//        ) { result: ActivityResult ->
//            Log.d("result", "result was reached by contract")
//            if (result.resultCode == Activity.RESULT_OK) {
//                Log.d("result", "result was reached by contract")
//                val data: Intent? = result.data
//            }
//        }
//
//        startForResult.launch(intent)
        startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d("ACTIVResult", "ActivityResult was called")
    }
}

In my 2nd activity, I have written code as follows:

class NoteViewActivity: AppCompatActivity() {
    private var note: Note? = null

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                saveChangeAndFinish()
                return true
            }
        }

        return super.onOptionsItemSelected(item)
    }

    fun saveChangeAndFinish() {
        val resultIntent = Intent()
//        resultIntent.putExtra("updatedNote", note)
        resultIntent.putExtra("test", "test")
        setResult(RESULT_OK, resultIntent)
        Log.d("resultIntentV2", resultIntent.extras.toString())
        finish()
    }
}

I've tried both startActivityForResult() <- which is deprecated -> and the newer method of using register(...), as i've commented out above. I'm beginning to wonder if its some weird setting in my manifest or something, I don't know.. I've been trying to fix this for over an hour and no luck.?

I've seemingly exhausted all possible solution routes except asking for help, I looked at StackOverflow, and old forum posts on the web.


Solution

  • I found the problem. At top level of the class, as an instance variable, i had created a itemClickListener object that was overriding the function, and the activity was starting based off of that object's function and not the one in the code. Since the first was using startActivity(), it was not able to fire onActivityResult().

    Goofy mistake on my part after coding for so long. To fix it, i now made the registerForActivityResult() object in onCreate(), and then created the clicklistener object after:

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
    
            val startForResult = registerForActivityResult(
                ActivityResultContracts.StartActivityForResult()
            ) { result: ActivityResult ->
                Log.d("result", "result was reached by contract")
                if (result.resultCode == Activity.RESULT_OK) {
                    Log.d("result", "result was reached by contract")
                    val data: Intent? = result.data
                }
            }
    
            val itemClickListener = object : OnItemClickListener {
                override fun onItemClick(note: Note) {
                    val intent = Intent(this@MainActivity, NoteViewActivity::class.java)
                    val bundle = Bundle()
                    bundle.putParcelable("note", note)
                    intent.putExtras(bundle)
    
                    startForResult.launch(intent)
                }
            }