Foggy day
Android(안드로이드) - EventBus Example 본문
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);
}
}
'Android' 카테고리의 다른 글
Android(Kotlin) - question example using assets file - 2 (0) | 2021.03.14 |
---|---|
Android(Kotlin) - question example using assets file - 1 (0) | 2021.03.14 |
Android(java) - exoplayer2 (0) | 2021.03.05 |
Android(java) - Activity popup(background dim) (0) | 2021.03.04 |
Android(java) - Depending on seekBar progress num, change view size using BottomSheet (0) | 2021.03.04 |