0

I have initialized 2 public Receiver variable at beginning of my register.class

public RegisterReceiver rReceiver;
public ConvertAddressToLatLongReceiver catllReceiver;

and then in onCreate() I defined them as follows

rReceiver = new RegisterReceiver(new Handler());
rReceiver.setReceiver(this);

catllReceiver = new ConvertAddressToLatLongReceiver(new Handler());
catllReceiver.setReceiver(this);

Both of these Receivers implement onReceiveResult() at the end of the class. Is there a way to distinguish which of the services is calling the onReceiveResult() fucntion like distinguishing between which buttons are clicked in onClick() ?

Edited: this is one of my Receiver classes.

import ca.amoh.track.trackingnetwork.interfaces.Receiver;


    public class RegisterReceiver extends ResultReceiver {

    private Receiver rReceiver;

    public RegisterReceiver(Handler handler) {
        super(handler);
    }

    public void setReceiver(RegisterScreen rReceiver) {
        this.rReceiver = rReceiver;
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (rReceiver != null) {
            rReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}
amoh
  • 163
  • 12
  • You can use the `resultCode` parameter or otherwise put some marker info in the `Bundle` parameter. Is both the receiver your private classes or part of some library? It's bit difficult to understand as you have not put the origin of these Receiver classes. – dhaval Sep 14 '15 at 15:38
  • Is `ResultReceiver` and `ConvertAddressToLatLongReceiver` are `BroadcastReceiver`?? If yes then suggestion in my last comment won't work. – dhaval Sep 14 '15 at 15:46
  • A marker info in the bundle will work great! And yea my receiver classes are private classes that extends ResultReceiver. – amoh Sep 14 '15 at 15:48
  • Ok. I have put same in answer if that works for you. – dhaval Sep 14 '15 at 15:49

1 Answers1

1

This is the structure of the onReceiveResult method:

protected void onReceiveResult(int resultCode, Bundle resultData) {
    super.onReceiveResult(resultCode, resultData);
}

Now you can either use the resultCode parameter to distinguish between two results or use the Bundle parameter with some marker information.

Edit

If you use the resultCode then it is slightly easy to switch between results:

protected void onReceiveResult(int resultCode, Bundle resultData) {
    switch(resultCode){
        case 100: \\ result from one receiver
             ...                 
        case 200: \\ result from another receiver
             ...
    }
}
dhaval
  • 7,611
  • 3
  • 29
  • 38