问题描述
我试图用活动与片段之间greenrobot传递数据,但我无法找到合适的教程,说明如何在细节做到这一点。
根据我迄今读到我写了一些这样的东西,但它并不work.how我可以用绿色的机器人将数据传递到尚未inialized又一个活动或片段?
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:
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:
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();
}
事件处理程序不会被调用here.what我哪里做错了?
The event handler is never called here.what am I doing wrong?
推荐答案
EventBus对发布和注册events.In哪些活动或片段尚未初始化,我们可以使用registerSticky和postSticky而不是寄存器和岗位的情况下两种方法。
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.
下面是我自己的修正code:
here is my own corrected code:
MainActivity:
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:
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();
}
这篇关于如何使用greenrobot传递数据到活动或片段,其尚未被初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!