daicon daicon - 2 years ago 83
Android Question

Broadcast received multiple times to close and open activity

I have the following problem:
A fragment is getting me multiple times one broadcastRecivier:

onCreate my fragment:

public void onCreate(Bundle savedInstanceState) {

// Filtro de acciones que serán alertadas
IntentFilter filter = new IntentFilter("serv_connected");

// Crear un nuevo ResponseReceiver
receiver = new ResponseReceiverFragment();

// Registrar el receiver y su filtro
activity = ((SocialActivity) getActivity());


Here the broadcast, this within my fragment;

// Broadcast receiver que recibe las emisiones desde los servicios
private class ResponseReceiverFragment extends BroadcastReceiver {

// Sin instancias
private ResponseReceiverFragment() {

public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {

case "serv_connected":
MyService aux = activity.getmService();
mUserList = MyService.xmpp.getOnlineUsers();



The fragment is within an activity containing 3 fragment with sliding tabs, nothing rare.

The problem is the following:
When I enter and broadcast activity is triggered, the fragment receives correctly.
Now, if I come back, (closing activity) and I open the activity again, the fragment receives twice the broadcast, and so on... etc etc.

What's going on?
If you need more code, I can add.

Thank you

Answer Source

I managed to solve successfully thanks to the comments:

1º - It is necessary to unregister in onDestroy o OnPause:

        getActivity().registerReceiver(receiver, filter);

2º When launching the broadcast he did so:


But it is necessary to do so:


Still I do not understand why (1 month ago I program in android)

If anyone can explain it appreciate.

a greeting

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download