Search code examples
androidandroid-activitykotlinfragment

passing data from fragment to activity is not same


I have fragment in activity. and I want to send data from fragment to activity. enter image description here

how I get data in Fragment and send to my Activity:

val edit = question!!.id
(activity as QuestionActivity).kirimItem(edit)

in activity :

fun kirimItem(item: String) {
    idItem = item
}

and I call idItem in the button next onClick and show Toast the value from idItem

in the fragment, when I test data, question!!.id = 8 but toast is showing 11. The question is, why passing data from fragment to activity is not same. please guide me:(


Solution

  • I am not sure that what is the rest of our code. This piece of code below works for me.

    // Fragment, `btn` is Button and `edt` is EditText
    btn.setOnClickListener {
        (activity as MainActivity?)?.callToast(edt.text.toString())
    }
    
    // Activity
    fun callToast(str: String) {
        Toast.makeText(this, str, LENGTH_SHORT).show()
    }