Java 是否未调用OneEvent()?
我正在将数据从Java 是否未调用OneEvent()?,java,android,android-fragments,android-asynctask,android-tabbed-activity,Java,Android,Android Fragments,Android Asynctask,Android Tabbed Activity,我正在将数据从AsyncTask传递到选项卡式活动的片段。为什么未调用EventBus的onEvent() MainActivity调用BackgroundWorker.javadoInBackground() public class BackgroundWorker extends AsyncTask<String,Void,String> { @Override protected void onPostExecute(String line) { us
AsyncTask
传递到选项卡式活动的片段。为什么未调用EventBus的onEvent()
MainActivity调用BackgroundWorker.java
doInBackground()
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);
}
}
}
尝试了LocalBroadcastManager,结果相同,
onReceive()
未被调用。我哪里出错了?如果您在onPostExecute
中启动活动,我建议通过意图而不是事件来传递数据-这样更简单。
您订阅的方法不会被调用,因为事件是在您注册之前发布的-使用poststick
而不是post
,您将获得一个事件
我假设,您希望传递行
(字符串
)。因此,构建意图:
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("LINE", line);
context.startActivity(intent);
然后,在onCreate读取传递值的选项卡Activity
中:
String line = getIntent().getStringExtra("LINE", "");
如果您在
onPostExecute
中启动活动,我建议您按意图传递数据,而不是按事件传递数据—这样更简单。
您订阅的方法不会被调用,因为事件是在您注册之前发布的-使用poststick
而不是post
,您将获得一个事件
我假设,您希望传递行
(字符串
)。因此,构建意图:
Intent intent = new Intent(context,TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("LINE", line);
context.startActivity(intent);
然后,在onCreate读取传递值的选项卡Activity
中:
String line = getIntent().getStringExtra("LINE", "");
正如我所建议的,使用intent,而不是
EventBus
。如何在没有EventBus的情况下使用poststick
。你能给出使用意图的代码吗?我是Android的初学者,这很有帮助。然后,我使用bundle将数据从选项卡式活动传递到片段。这是正确的方法EventBus
是一个很好的库,但不是无处不在。正如我建议的,使用intent,而不是那里的EventBus
。如何在没有EventBus的情况下使用postSticky
。你能给出使用意图的代码吗?我是Android的初学者,这很有帮助。然后,我使用bundle将数据从选项卡式活动传递到片段。这是正确的方法EventBus
是一个有用且不错的库,但并非无处不在。