3

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

onCreate my fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

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

    // Registrar el receiver y su filtro
    LocalBroadcastManager.getInstance(getContext()).registerReceiver(
            receiver,
            filter);
    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() {
    }

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

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

                iconloader.hide();
                break;

        }
    }
}

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

daicon
  • 181
  • 3
  • 15
  • I assume your fragments are not being cached or having their state saved, hence, Android will keep firing `onCreate()` - Add some logging in to output when `onCreate()` is called and from there, you'll need to work out how to manage the state – brandall Sep 30 '16 at 14:28
  • As I can know the status? I know, I'm not keeping anything or caching: -S .... But it would not be safe. How I can check? Thank you – daicon Sep 30 '16 at 14:43
  • Add logging in `onCreate()` and check the logcat to see how many times it is called and when it is called - for instance, every time you slide between tabs etc. Once you understand the behaviour, search for a post on how to save Fragment state using sliding tabs. – brandall Sep 30 '16 at 14:46
  • I have done. But nothing .... Only once created the fragment even close and open several times the activity. I think that is not the problem: -S – daicon Sep 30 '16 at 14:57
  • 1
    Are you unregistering the receiver in `onDestroy()' of your Fragment – brandall Sep 30 '16 at 15:08
  • no .....: -S buah .... now it works correctly. But there is something I do not understand. I had to change something else: In the line where I shot the broadcast had this:              LocalBroadcastManager.getInstance (getBaseContext ()) sendBroadcast (localIntent).; I had to change it to:              getApplicationContext () sendBroadcast (localIntent).; And I do not understand the reason well. Would you know tell me? Thank you – daicon Sep 30 '16 at 15:29
  • The reason is. I was using the same inner class name, which extended the broadcast receiver into several activities. I suffered for a month to figure it out and just fixed it by renaming the inner class name which extends BroadcastReceiver. – MD. Shafiul Alam Biplob Jun 17 '22 at 00:45

4 Answers4

3

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:

            LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(localIntent);

But it is necessary to do so:

            getApplicationContext().sendBroadcast(localIntent);

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

If anyone can explain it appreciate.

a greeting

daicon
  • 181
  • 3
  • 15
2

I had the same problem and a dealt it with a really simple solution.I just added a flag value to check if broadcast is already triggered or not.

boolean broadcastTriggerd = false;
@Override
public void onReceive(Context context, Intent intent) {
 if(!broadcastTriggered){
    broadcastTriggerd = true;
    switch (intent.getAction()) {

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

            iconloader.hide();
            break;

       }
   }
}
ujjwal mainali
  • 379
  • 1
  • 5
  • 17
  • Thank you for helping. But the solution is not correct. The problem still exists. That is, oki now only runs once the code I want. But the broadcast is still sent as many times as I close and open the activity .... :-( I want to understand and solve this problem. Thank you – daicon Sep 30 '16 at 14:14
  • I assume you haven't unregistered the broadcast receiver onDestroy() method of activity? – ujjwal mainali Sep 30 '16 at 16:29
1

As discussed in the comments, the issue is you're not unregistering the receiver in OnDestroy() of your Fragment.

In regards to the context in which you register the receiver, it's not a simple answer but you can read in more detail here

Community
  • 1
  • 1
brandall
  • 6,094
  • 4
  • 49
  • 103
0

My issue resolved when removed the receiver unregister in onPause()

shiny vn
  • 91
  • 3