title: Get current activity from Application.Context - MonoAndroid
body: <p>I am currently developing an application using <code>Xamarin.Forms</code> that will be available on the Android and iOS platforms. When the application is first loaded on device, I check to see if there is an internet connection available on the device. I want to display a dialog box if an internet connection is not available.</p>
  
  <p>Here is the following snippet of code I am using to check the internet on the <code>Xamarin.Forms.ContentPage</code></p>
  
  <pre><code>if(App.Connectivity.IsNetworkConnectivityAvailable())
  {
      App.Notification.DisplayLocalNotifications("No Internet", "You need an internet connection to access certain application content");
  }
  </code></pre>
  
  <p>I am using dependency injection to build the appropriate module for handling dialog boxes for each appropriate environment. The Android is throwing the following exception</p>
  
  <blockquote>
    <p>Android.Views.WindowManagerBadTokenException: Unable to add window --
    token null is not for an application Here is the code for the
    DisplayLocalNotification method on the Android:</p>
  </blockquote>
  
  <pre><code>public void DisplayLocalNotification(string title, string content)
  {        
       AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context)
            .SetTitle(title)
            .SetMessage(content)
            .SetCancelable(true)
            .SetPositiveButton("OK", (EventHandler&lt;DialogClickEventArgs&gt;) null);
  
        AlertDialog alert = builder.Create();
        alert.Show();
  
        var okBtn = alert.GetButton((int)DialogButtonType.Positive);
  
        okBtn.Click += (sender, args) =&gt;
        {
             alert.Dismiss();
        };
  }
  </code></pre>
  
  <p>After doing some research, I need to get pass the current activity to the <code>AlertDialog.Builder</code> constructor instead of the <code>Application.Context</code>. How do I get the current activity object from the application context when you need to the activity outside of the activity context?</p>