我正在将数据传递AsyncTask
给选项卡式活动的片段。为什么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
此文件是选项卡式活动的片段。是否应该在选项卡式活动中完成任何事情,或者必须再次调用 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()
没有被调用。我哪里错了?