Search code examples
androidandroid-fragmentsandroid-activityinterfacebroadcastreceiver

Interface in BroadcastReceiver call only activity instead of fragment


I've implemented a custom interface inside my NetworkReceiver

public interface onNetworkStateChanged {
    void onStateChange(int state);
}

I have set both of them on my Activity and Fragment

network = new NetworkReceiver();
network.setOnNetworkStateChanged(this);

---------------------------------------------

@Override
public void onStateChange(int state) {
    if (state == NetworkReceiver.STATE_ON) {
        networkState = state;
    } else {
        networkState = state;
    }
}

And when the NetworkReceiver is trigger, it's always the listener inside the Activity who will be called and never the one in the fragment

Anyone have a idea ? or i need to implement a customCallback inside my fragment who the activity will trigger to call the Fragment ?

EDIT :

i'v put this on the onStateChange on the Activity

Fragment f = getFragmentManager().findFragmentByTag(Utils.FEED_FRAGMENT);
if (f instanceof FeedFragment) {
    ((FeedFragment) f).onStateChange(state);
}

Solution

  • Hard to say, because your code is incomplete. You should register a Fragment as a listener, seems setOnNetworkStateChanged(this) uses Activity instance.

    Or in the Activity you can find Fragment and directly call onStateChange(), so you'll get chained calls (same mechanics as onOptionsItemSelected).