Forum Xamarin Xamarin.Android

Context parameter in broadcastreceiver onreceive method changes after first message received

burritoburrito Member ✭✭

I'm seeing some really odd behavior on my broadcastreceiver on a One Plus Two device running Marshmallow in a Xamarin project. I have a service that I'm using for location changes and I'm registering a broadcastreceiver in my activity to listen for messages from the service. The first time a message is received, the context parameter is correct (the context of the mainactivity that registered the receiver). The second time a message is received however, the context is the application context and so I'm unable to update anything on the mainactivity. I'm also testing with a Galaxy S8 Active running Oreo and on that device, the context is correct each time.

UPDATE: this is also happening on a galaxy s7 device also running marshmallow.

I've also built this same application in Android Studio using an anonymous type for my broadcastreceiver, and in there, the context is correct each time on both devices.

is this a possible xamarin bug? I'm at a complete loss of where to turn next. I don't have any other devices with which to test, but I assume that other devices running older versions of android will have the same problem as this One Plus Two.

also, for what it's worth, I've tried three different ways of updating the mainactivity from the receiver.
1) passing an instance of the mainactivity to the broadcastreceiver's constructor and then calling methods off of that set property
2) casting the context to the mainactivity and calling methods from there
3) creating a custom eventhandler and adding an event inside the broadcastreciver and then adding a delegate to the instance of the receiver and fetching the values I need from the custom eventhandler.

All three work great, but only with the first message. With a different context coming through on subsequent messages, obviously none of them work.

Sign In or Register to comment.