0

I need to send a data from fragment B to A and add items to the Recyclerview inside of the Fragment A, so I'm using the EventBus to send the data and updating the list, the problem is, it only works in Debugging line by line, I mean, step by step and its weird.

So in the Fragment with the RecyclerView I register the Event and call the fragment.

override fun onStart() {
    super.onStart()
    if(!EventBus.getDefault().isRegistered(this)){
        EventBus.getDefault().register(this)
    }
}

 @Subscribe(threadMode = ThreadMode.ASYNC)
 fun onMessageEvent(event: TEvent?) {
    adapter?.addItems(event?.model!!);
    adapter?.notifyDataSetChanged();
}

The Adapter :

fun addItems(model: SubCategoryModel){
    dataM.run {
        add(model)
        if(dataM.isNotEmpty()){
            text.visibility = View.GONE;
            imageView.visibility = View.GONE;
        }
    }
}

Fragment B where I fill the form with the information

EventBus.getDefault().post(TEvent(model))
activity?.onBackPressed()

Before using the EventBus I was using a Dialog in the same Fragment and it worked but now, it only updates the list if the app is running in a debug mode.

How can I update the other fragment thread ?

Nathiel Paulino
  • 534
  • 6
  • 17

1 Answers1

0

I think you should update UI from MAIN threadMode instead of ASYNC

@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: TEvent?) {
     adapter?.addItems(event?.model!!);
     adapter?.notifyDataSetChanged();
}

UPDATE

May be swapping the post statement

activity?.onBackPressed() // makes receiver fragment start
EventBus.getDefault().post(TEvent(model))
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37