You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一Fragment中Otto订阅发布失效及多Fragment间通信问题咨询

Hey there! Let's work through your EventBus and Otto issues step by step:

Fixing EventBus Communication Between FragmentA and FragmentB

First, let's address the mistakes in your current EventBus setup:

  • You're registering EventBus twice (in both onDestroy() and onActivityCreated()) and never unregistering it. This causes duplicate registrations, memory leaks, and potential runtime exceptions. Registrations must always be paired with a corresponding unregistration.
  • Your registerEventBus() method is misnamed—it's actually posting an event, not registering. Most importantly, FragmentB isn't subscribed to receive the int event, so it can't pick up the data you're sending.

Correct FragmentA Implementation

// FragmentA
@Override
public void onStart() {
    super.onStart();
    EventBus.getBus().register(this); // Register when the fragment becomes visible
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getBus().unregister(this); // Unregister to avoid leaks
}

// Call this method when you want to send data to FragmentB (e.g., button click)
private void sendIntToFragmentB(int data) {
    EventBus.getBus().post(data); // Publish the int event
}

Required FragmentB Implementation (to receive the event)

FragmentB needs to register itself and define a subscription method to catch the int data:

// FragmentB
@Override
public void onStart() {
    super.onStart();
    EventBus.getBus().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getBus().unregister(this);
}

// Subscription method—must use the @Subscribe annotation
@Subscribe(threadMode = ThreadMode.MAIN) // Ensure this runs on the main thread for UI updates
public void onIntReceived(int receivedInt) {
    // Handle the received int here (e.g., update UI, run business logic)
    Log.d("FragmentB", "Received int value: " + receivedInt);
}
Fixing Otto Subscription/Publishing in the Same Fragment

If Otto isn't working within a single Fragment, the issue is almost always related to incorrect registration timing, mismatched Bus instances, or using primitive types directly. Here's how to fix it:

Step 1: Use a Singleton Bus Instance

Never create a new Bus instance every time you need it—use a single global instance:

public class BusProvider {
    private static final Bus BUS = new Bus();

    public static Bus getInstance() {
        return BUS;
    }

    private BusProvider() {} // Prevent accidental instantiation
}

Step 2: Correct Registration/Unregistration Pairing

Pair your registration and unregistration with lifecycle methods that ensure the Fragment is active when subscribing:

// Your Fragment
@Override
public void onResume() {
    super.onResume();
    BusProvider.getInstance().register(this);
}

@Override
public void onPause() {
    super.onPause();
    BusProvider.getInstance().unregister(this);
}

Step 3: Use a Custom Event Class (Avoid Primitive Types)

Otto works best with custom event classes (primitive types can cause matching issues). Define a simple wrapper for your int data:

public class IntEvent {
    private final int value;

    public IntEvent(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

Step 4: Publish and Subscribe to the Event

// Publish the event (trigger this from your Fragment's logic)
private void publishIntEvent() {
    BusProvider.getInstance().post(new IntEvent(42)); // Example value
}

// Subscribe to the event—use the @Subscribe annotation
@Subscribe
public void onIntEventReceived(IntEvent event) {
    int receivedValue = event.getValue();
    // Handle the data here
    Log.d("MyFragment", "Received value via Otto: " + receivedValue);
}

Common pitfalls to avoid:

  • Using different Bus instances for publishing and subscribing (always use the singleton from BusProvider).
  • Forgetting to unregister—this causes memory leaks and broken subscriptions.
  • Trying to subscribe after the Fragment has been paused/destroyed.

内容的提问来源于stack exchange,提问作者JoCuTo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:30:48