The eventbus library is not working no matter what I do

50 views Asked by At

I wanted to use the eventbus library as an example, but it doesn't work, what do you think?

class MainActivity : AppCompatActivity() {

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

        val btn1 = findViewById<Button>(R.id.button)

        btn1.setOnClickListener {

            EventBus.getDefault().post(SendEvent("Meara"))

            startActivity (  Intent ( this , ActivityOne::class.java ) )
        }
    }
}

Component that broadcasts the eventbus above

class SendEvent( string : String) {

    var str : String = string
}

a class that holds data

Finally there is a component that receives the data

class ActivityOne : AppCompatActivity() {

    private lateinit var txt : TextView

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

        EventBus.getDefault().register(this)

        txt = findViewById(R.id.txt1)
    }

    override fun onDestroy() {

        EventBus.getDefault().unregister(this)

        super.onDestroy()
    }

    @Subscribe
    fun onEvent (event : SendEvent ) {

        txt.text = event.str
    }
}
0

There are 0 answers