简体   繁体   English

onEvent()没有被调用?

[英]onEvent() is not being called?

I am passing data from AsyncTask to a fragment of tabbed activity. 我将数据从AsyncTask传递到选项卡式活动的片段。 Why onEvent() of EventBus is not being called? 为什么没有调用EventBus的onEvent()

BackgroundWorker.java doInBackground() is called by MainActivity 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 This file is a fragment of tabbed activity. ProfileTab.java此文件是选项卡式活动的片段。 Is there anything that should be done in tabbed activity or should doInBackground must be called again. 是否应该在选项卡式活动中执行任何操作,或者是否必须再次调用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());
  }

}

Tried LocalBroadcastManager and the result was same, onReceive() was not called. 尝试了LocalBroadcastManager并且结果相同,未调用onReceive() Where am I going wrong? 我哪里错了?

If you start Activity in your onPostExecute , I suggest pass data by intent, not events - it is simpler. 如果你在onPostExecute启动Activity,我建议按意图传递数据,而不是事件 - 它更简单。 Your subscribed method isn't called, because event is posted before you register - use postSticky instead of post , and you will get an event. 您的订阅方法未被调用,因为事件在您注册之前发布 - 使用postSticky而不是post ,您将获得一个事件。

I assume, you want to pass line ( String ). 我假设你要传递lineString )。 So construct intent: 所以构建意图:

Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("LINE", line);
context.startActivity(intent);

Than, in your TabActivity in onCreate read passed value: 比,在你的TabActivity中onCreate读取传递的值:

String line = getIntent().getStringExtra("LINE", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM