onEvent() 没有被调用?
onEvent() is not being called?
我正在将数据从 AsyncTask
传递到选项卡式 activity 的片段。为什么 onEvent()
的 EventBus 没有被调用?
BackgroundWorker.java
doInBackground() 被 MainActivity
调用
public class BackgroundWorker extends AsyncTask<String,Void,String> {
@Override
protected void onPostExecute(String line) {
userResult = line.split(" ");
String result = userResult[0];
if(result.equals("Success")){
CustomMessageEvent event = new CustomMessageEvent();
event.setCustomMessage(line);
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
EventBus.getDefault().post(event);
}
}
}
ProfileTab.java
此文件是选项卡式 activity 的片段。有什么应该在选项卡中完成的 activity 或者必须再次调用 doInBackground.
public class ProfileTab extends Fragment {
public TextView t1;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
t1 = (TextView) rootView.findViewById(R.id.textView4);
EventBus.getDefault().register(this);
Log.d(TAG, "onCreateView: ");
return rootView;
}
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onEventAsync(CustomMessageEvent event) {
Log.d(TAG, "onEvent: ");
t1.setText(event.getCustomMessage());
}
}
尝试了 LocalBroadcastManager,结果是一样的,onReceive()
没有被调用。我哪里错了?
如果您在 onPostExecute
中开始 Activity,我建议按意图传递数据,而不是事件 - 它更简单。
您订阅的方法未被调用,因为事件在您注册之前发布 - 使用 postSticky
而不是 post
,您将获得一个事件。
我假设,您想通过 line
(String
)。所以构造意图:
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("LINE", line);
context.startActivity(intent);
然后,在你的 TabActivity
中 onCreate 读取传递的值:
String line = getIntent().getStringExtra("LINE", "");
我正在将数据从 AsyncTask
传递到选项卡式 activity 的片段。为什么 onEvent()
的 EventBus 没有被调用?
BackgroundWorker.java
doInBackground() 被 MainActivity
public class BackgroundWorker extends AsyncTask<String,Void,String> {
@Override
protected void onPostExecute(String line) {
userResult = line.split(" ");
String result = userResult[0];
if(result.equals("Success")){
CustomMessageEvent event = new CustomMessageEvent();
event.setCustomMessage(line);
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
EventBus.getDefault().post(event);
}
}
}
ProfileTab.java
此文件是选项卡式 activity 的片段。有什么应该在选项卡中完成的 activity 或者必须再次调用 doInBackground.
public class ProfileTab extends Fragment {
public TextView t1;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
t1 = (TextView) rootView.findViewById(R.id.textView4);
EventBus.getDefault().register(this);
Log.d(TAG, "onCreateView: ");
return rootView;
}
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onEventAsync(CustomMessageEvent event) {
Log.d(TAG, "onEvent: ");
t1.setText(event.getCustomMessage());
}
}
尝试了 LocalBroadcastManager,结果是一样的,onReceive()
没有被调用。我哪里错了?
如果您在 onPostExecute
中开始 Activity,我建议按意图传递数据,而不是事件 - 它更简单。
您订阅的方法未被调用,因为事件在您注册之前发布 - 使用 postSticky
而不是 post
,您将获得一个事件。
我假设,您想通过 line
(String
)。所以构造意图:
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("LINE", line);
context.startActivity(intent);
然后,在你的 TabActivity
中 onCreate 读取传递的值:
String line = getIntent().getStringExtra("LINE", "");