5

Currently my project used EventBus to post event and I am trying to replace LiveData with EventBus. In theory, they work similarly. I migrated without no pain in the beginning. But it comes up a scenario I have no idea how to deal with it.

And here's is the scenario.

LaunchActivity -> PersonInfoActivity -> UpdateInfoActivity

Currently We use EventBus to subscribe UserInfoEvent in LaunchActivity and PersonInfoActivity

LaunchActivity.java

public class LaunchActivity{
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onEvent(UserInfoEvent event){

     }
}

PersonInfoActivity.java

public class PersonInfoActivity{
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onEvent(UserInfoEvent event){

     }
}

UpdateInfoActivity.java

public class UpdateInfoActivity{
     public void onSubmit(){
          EventBus.getDefault().post(new UserInfoEvent());
     }
}

And the question is , I want to use LiveData to replace this scenario

What I have done?

I have read about the question about singleton usage with livedata Room - LiveData observer does not trigger when database is updated, I want to try to use the same way but no luck. The Event is fire everytime in onChanged() when I started the Activity

Long Ranger
  • 5,888
  • 8
  • 43
  • 72
  • 2
    May I ask why have you decided to replace EventBus with LiveData? – FelisManulus Nov 23 '19 at 00:40
  • @FelisManulus That's his case/need and we shouldn't bother with that - this is silly question. Are you trying to advocate the use of LiveData? Just because Google promotes it and majority of developers and companies use it, that doesn't mean one should use it. – FEBRYAN ASA PERDANA May 18 '21 at 23:56

4 Answers4

3

You can achieve it with the use of viewModel implementation. Like,

public class UserViewModel extends AndroidViewModel {
private UserRepository userRepository;
private LiveData<List<UserData>> getUser;

public UserViewModel(@NonNull Application application) {
    super(application);
    userRepository = new UserRepository(application);
    getUser= userRepository.getUser();
}


public LiveData<List<UserData>> getUser() {
    return getUser;
}
}

After creating this class, Update your PersonalInfoActivity and LaunchActivity like,

private UserViewModel userViewModel;

    userViewModel.getUser().observe(this, new Observer<List<UserData>>() {
    @Override
public void onChanged(@Nullable final List<UserData> user) {
//write code to set data of user to show using list of user
}
});

Kindly leave commnet if you find any query regarding this.

Seeya
  • 91
  • 2
2

I think there is some misinterpretation regarding LiveData. You can have a look here

It might help you.Also you can check this for event bus implementation with LiveData event bus with liveData

Hope it helps!!!

Community
  • 1
  • 1
Anu Bhalla
  • 422
  • 4
  • 11
  • Actually I have read this article about the alternatives but I think it will brings up confusion if I use "LiveData" and "EventBus" at the same time. There is more SingleEvent, EventWrapper suggestions from Google Developer using LiveData. So I think finally the LiveData will replace the EventBus. But one thing I get stuck is, the new obsever attached from the new Activity/Fragment keep getting the last data from the static Bus Class, which is I dun want and also not same to the EventBus Behaviour. – Long Ranger Jul 16 '18 at 09:44
1

I created a library called LivedataBus from the tutorial .You can use it like this:

 //Subscribe
 LiveDataBus.subscribe("event_name", this, Observer {
        it.runAndConsume {
            Toast.makeText(this, "Hello ${it.value}", Toast.LENGTH_LONG).show()
        }
    })

 //Publish
 val obj = SomeEvent()
 LiveDataBus.publish("event_name", ConsumableEvent(value = obj))

The magic is that I used the ConsumableEvent, which can be consumed after firing event, prevent to get the last data from the static Bus.
I use it in my current project, and it works well on multiple activities & fragments. The benefit of this lib is that it works on Livedata, and you don't need to care about activity or fragment lifecycle.

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
0

You can declare and initialize the live data instance in the singleton class. And can use the same instance in all three different activities.