Foggy day

Android(안드로이드) - EventBus Example 본문

Android

Android(안드로이드) - EventBus Example

jinhan38 2021. 3. 10. 15:29

 

 

 

 

video

 

 

 

 

 

 

1. activity_event_bus_sample_kotlin.xml

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


    <Button
        android:id="@+id/btnEventPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="데이터 전달"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvEventNickName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnEventPost" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

 

2. EventBusSampleActivityKotlin

class EventBusSampleActivityKotlin : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_event_bus_sample_kotlin)
        GlobalBus.bus!!.register(this)
        val eventBusNickname = EventsNickname("jinhan")

        btnEventPost.setOnClickListener {

            GlobalBus.bus!!.post(eventBusNickname)

        }

    }

    @Subscribe
    fun getBusData(eventBusNickname: EventsNickname) {
        tvEventNickName.text = eventBusNickname.nickname
    }

    override fun onDestroy() {
        super.onDestroy()
        GlobalBus.bus!!.unregister(this)
    }

}

 

 

 

 

 

3. EventBusSampleActivityJava

public class EventBusSampleActivityJava extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus_sample_kotlin);

        GlobalBus.INSTANCE.getBus().register(this);

        EventsNickname eventsNickname = new EventsNickname("Jinhan");

        ((Button) findViewById(R.id.btnEventPost)).setOnClickListener(view -> {

            GlobalBus.INSTANCE.getBus().post(eventsNickname);

        });

    }

    @Subscribe
    public void getBusData(EventsNickname eventsNickname) {

        ((TextView) findViewById(R.id.tvEventNickName)).setText(eventsNickname.getNickname());

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        GlobalBus.INSTANCE.getBus().unregister(this);

    }

}