MinT-0548 avatar image
0 Votes"
MinT-0548 asked MinT-0548 edited

C# Android: passing the current activity's context to an event handler

i have a C# android app that has multiple activities. im trying to implement a way to have a popup for the user when the app encounters connectivity issues. i implemented the Xamarin Essentials Connectivity class ( so i can catch a connectivity event. in the main activity i added the event handler :

  override void OnCreate(Bundle bundle)
             Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged; 

each activity, i added an event handler to catch the event and do a popup to the user.

 private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
             var access = e.NetworkAccess;
             CIMCommon.WriteDebugMsg("Connectivity_ConnectivityChanged: access=" + access);
             CIMCommon.PopUpInfoBox("THE NETWORK HAS CHANGED TO: " + access, "NETWORK WARNING", this);
             SplashActivity.SayTTSMessage("NETWORK CONNECTIVITY WARNING");

this popup requires a Context so i was passing "this" on each page so the popup gets called on the current screen.

however, when it runs, it gets a windowmanagerbadtokenexception. digging into debug mode i noticed that when i view the "this" context im passing in, it always is passing the previous context instead of the current one (ie the screen it was at before). im not having this issue with other popups on that screen that use the passed in current context so im wondering if im implementing this wrong or if theres a way to pass in the current context?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered MinT-0548 edited


Welcome to our Microsoft Q&A platform!

You can use var context= Android.App.Application.Context; to replace this, it will give the application level's context(It is equivalent to getApplicationContext() in Native android).

And I notice you used Xamarin.Essentials please try to use var context = Xamarin.Essentials.Platform.AppContext; code to get context to replace this.

Best Regards,

Leon Lu

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

tried it with both and both give a new error:

You need to use a Theme.AppCompat theme (or descendant) with this activity

0 Votes 0 ·

Could you share a demo that reproduce this issue?

0 Votes 0 ·

ok i think i figured it out. it looks like i need to unsubscribe to the event handler on each screen before moving to the next activity. i originally only had 1 registered on the first activity and have every activity have the event handler, but it was only passing the first context to the event handler.

i am now subscribing and unsubscribing to the event handler on each activity and it looks to be passing the correct context to the popup.

0 Votes 0 ·