Xamarin.Forms: Forms.Context deprecated

The new obsolete warning in Xamarin.Forms 2.5 really puzzled me. What context should I use in dependency services, for example, to call GetSystemService ()?

Should I store the activity context in a static field, were the xamarin forms initialized against?

Should I override the Android application class and use its Context?

Should I call GetSystemService when creating an action and save it somewhere?

+9
xamarin.forms
source share
1 answer

I had the same issue with multiple Dependency Services

Simplest solution

In many cases, for single-activity applications

 Android.App.Application.Context 

Can be replaced by

 Xamarin.Forms.Forms.Context 

Background in more detail

Android.App.Application.Context returns the global application context of the current process, bound to the application life cycle, as described in the Activity context.

A typical example of using an application context is to trigger an action, for example.

 Android.App.Application.Context.StartActivity(myIntent); 

A general rule is to use the current activity context if you do not need to save a link to the context from an object that lives outside of your Event. In this case, use the application context

Why is Forms.Context deprecated?

Xmarin.Forms 2.5 has introduced a new “Embed Forms” feature that can embed form pages in Xamarin.iOS / Xamarin.Android applications. However, since Xamarin.Android applications can use several activities, it would seem that there is a danger that Xamarin.Android users calling Forms.Context will refer to MainActivity , which has potential problems.

Work around

Inside Renderer, you will now receive a link to the view context, which is passed to the constructor.

With any other class, you are faced with the question of how to get an activity context. In a separate application (in most cases) Application.Context will work fine.

However, to get the current context of activity in an application with several actions, you will need to provide a link to it. The easiest and most reliable way to do this is through a class that implements the Application.IActivityLifecycleCallbacks interface.

The basic idea is to maintain a Context link when activity is created, started, or resumed.

 [Application] public partial class MainApplication : Application, Application.IActivityLifecycleCallbacks { internal static Context ActivityContext { get; private set; } public MainApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer) { } public override void OnCreate() { base.OnCreate(); RegisterActivityLifecycleCallbacks(this); } public override void OnTerminate() { base.OnTerminate(); UnregisterActivityLifecycleCallbacks(this); } public void OnActivityCreated(Activity activity, Bundle savedInstanceState) { ActivityContext = activity; } public void OnActivityResumed(Activity activity) { ActivityContext = activity; } public void OnActivityStarted(Activity activity) { ActivityContext = activity; } public void OnActivityDestroyed(Activity activity) { } public void OnActivityPaused(Activity activity) { } public void OnActivitySaveInstanceState(Activity activity, Bundle outState) { } public void OnActivityStopped(Activity activity) { } } 

Using the above approach, individual application applications and several Activity applications can now access the context of the current / local activity. for example, instead of relying on a global context

 Android.App.Application.Context // or previously Xamarin.Forms.Forms.Context 

Can now be replaced by

 MainApplication.ActivityContext 

Dependency Service Call Example

 if (MainApplication.ActivityContext!= null) { versionNumber = MainApplication.ActivityContext .PackageManager .GetPackageInfo(MainApplication.ActivityContext.PackageName, 0) .VersionName; } 

Additional resources

Android.App.Application.IActivityLifecycleCallbacks

registerActivityLifecycleCallbacks

+14
source share

All Articles