Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 245
armin Avatar asked Dec 30 '13 11:12

armin


People also ask

How do you send data between fragments?

When working with child fragments, your parent fragment and its child fragments might need to share data with each other. To share data between these fragments, use the parent fragment as the ViewModel scope.


3 Answers

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();
}
like image 54
armin Avatar answered Dec 07 '22 18:12

armin


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);
}
like image 38
Stefaan Ternier Avatar answered Dec 07 '22 19:12

Stefaan Ternier


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

like image 23
Jose_GD Avatar answered Dec 07 '22 18:12

Jose_GD