how to use greenrobot to pass data to activity or fragment that has not been initialized yet?
Asked Answered
S

4

10

I tried to use greenrobot pass data between activities and fragment,but I couldn't find a suitable tutorial that show how do it in detail. Based on what I have read so far I wrote some thing like this,but it doesn't work.how can I use green robot to pass data to an activity or fragment that has not been inialized yet?

MainActivity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventBus.getDefault().post(new String("We are the champions"));
    Intent intent = new Intent("com.test.Activity_Lessons");
    startActivity(intent);
}

Activity_Lessons :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Some initializations
    EventBus.getDefault().register(this);
    //Other Stuff
}

public void onEventMainThread(String s){
    Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}

The event handler is never called here.what am I doing wrong?

Semitone answered 30/12, 2013 at 11:35 Comment(0)
S
17

EventBus has two methods for posting and registering events.In cases which activity or fragment is not yet initialized,we can use registerSticky and postSticky instead of register and post.

here is my own corrected code:

MainActivity :

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventBus.getDefault().postSticky(new String("We are the champions"));
    Intent intent = new Intent("com.test.Activity_Lessons");
    startActivity(intent);
}

Activity_Lessons :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Some initializations
    EventBus.getDefault().registerSticky(this);
    //Other Stuff
}

public void onEventMainThread(String s){
    Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}
Semitone answered 28/1, 2014 at 11:21 Comment(2)
Make sure you remove the sticky event if you have other subscribers that could respond to it and you only want it to respond to it with one.Nuthouse
In EventBus v3, there is no registerSticky method, i just changed post() to postSticky() and added @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) above the onEventMainThread method then worked.Jenaejenda
A
2

I think you forgot to register your activity.

try to add the following: MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  EventBus.getDefault().post(new String("We are the champions"));
  EventBus.getDefault().register(this);
  Intent intent = new Intent("com.test.Activity_Lessons");
  startActivity(intent);
}

@Override
public void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}
Assault answered 27/1, 2014 at 15:9 Comment(0)
T
2

To add to Armin and David answers, I made postSticky work only after writing the subcriber's annotation like this :

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)

as stated in EventBus docs for Sticky Events

Tavarez answered 2/12, 2016 at 0:27 Comment(0)
S
1

Armin's answer (the first answer checked as accepted) is right.

But if you are using EventBus 3.0.0 (which is the latest version at the moment) or higher, you can't use this:

EventBus.getDefault().registerSticky(this);

That's because the registerSticky method is deprecated and removed, you can just use the register method like this:

EventBus.getDefault().register(this);

Hope this will help developers using up-to-date libraries and techiques. Cheers!

Sacrament answered 14/7, 2016 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.