You are doing at least 2 wrong things:
- Register the receiver through
Context.registerReceiver()
and then you're sending broadcast updates throughLocalBroadcastmanager
. You will never get these messages.LocalBroadcastManager
doesn't know about receivers registered throughContext
and vice-versa. - I don't think it's a good idea to register the receiver directly in the application context as this will leak the fragment.
So try this: register the receiver in onCreate
or onResume
and then unregister the same instance in the complementary method: onDestroy
or onPause
. Also, when registering and sending intents, use the same mechanism - either LocalBroadcastManager
, either the Context
based. The first one has the advantage of sending the messages only in your app.
For example, suppose your fragment is called DeviceView
:
public class DeviceView extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(your_receiver, intent_filter);
/// other code
}
@Override
public void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(your_receiver);
/// other code
}
}