10

I am currently developing an application using Xamarin.Forms 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.

Here is the following snippet of code I am using to check the internet on the Xamarin.Forms.ContentPage

if(App.Connectivity.IsNetworkConnectivityAvailable())
{
    App.Notification.DisplayLocalNotifications("No Internet", "You need an internet connection to access certain application content");
}

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

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:

public void DisplayLocalNotification(string title, string content)
{        
     AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context)
          .SetTitle(title)
          .SetMessage(content)
          .SetCancelable(true)
          .SetPositiveButton("OK", (EventHandler<DialogClickEventArgs>) null);

      AlertDialog alert = builder.Create();
      alert.Show();

      var okBtn = alert.GetButton((int)DialogButtonType.Positive);

      okBtn.Click += (sender, args) =>
      {
           alert.Dismiss();
      };
}

After doing some research, I need to get pass the current activity to the AlertDialog.Builder constructor instead of the Application.Context. How do I get the current activity object from the application context when you need to the activity outside of the activity context?

Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • Does Xamarin not have the basic concept of passing an object's instance by simply using `this`? – Squonk Sep 01 '14 at 21:55
  • @Squonk - Yes, it does but this code is not being called from class that inherits from the `AndroidActivity` object. It is contained within a class that does not know the current activity object, but can access the `Application.Context` object – Michael Kniskern Sep 01 '14 at 22:01
  • 1
    If you don't have an active `Activity` then you can't guarantee that one exists, Something must be active / visible in order to create the `AlertDialog` in which case you should be using that `Activity` `Context`. An `Application` in Android terms has no visibility and is basically a skeleton framework. – Squonk Sep 01 '14 at 22:23
  • Hope it help http://stackoverflow.com/a/28423385/185022 – AZ_ Feb 10 '15 at 03:09

1 Answers1

24

Xamarin.Forms Android platform code should assign the current Activity into Forms.Context property. This is the static Forms class and if you debug it you will see that the Forms.Context is an Activity.

public static class Forms
{
    public static Context Context { get; }
    public static bool IsInitialized { get; }

    public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;

    public static void Init(Activity activity, Bundle bundle);
}
SKall
  • 5,234
  • 1
  • 16
  • 25