1

Xamrin.Auth (https://github.com/xamarin/Xamarin.Auth), when used on Android, requires any method that accesses the account store to pass a Context to the call.

I am wondering if it's ok you always use the Application Context via getApplicationContext()

My use case is initializing an IOC at application startup and passing that context to my lib which manages Xamarin.Auth calls.

I am either missing it or the Xamarin.Auth docs don't say which context is appropriate to use: Application vs Activity.

AJ Venturella
  • 4,742
  • 4
  • 33
  • 62

1 Answers1

2

It is not always okay to use the application context as each context has a different "lifecycle" it's tied to. Wrong usage of Context can result in major memory leaks in your application.

In the case of initializing a library, you should always use the application context as it will span the lifecycle of the application.

However if you only need the context for the lifespan of an object in a Activity, then you should use the Activity's context.

Jon Douglas
  • 13,006
  • 4
  • 38
  • 51
  • 1
    Since my service container/registry is initialized in the Application's OnCreate() (not in the Activity) It probably makes the most sense then to use the `ApplicationContext` I am going to assume based on your information above. – AJ Venturella Jan 20 '18 at 02:55
  • 1
    That would be a good assumption. My rule of thumb is typically "Use the `Context` within the scope of where you're initializing". For example, if you needed a `Context` when initializing in the `Activity`, you would use the `Activity` `Context`. – Jon Douglas Jan 20 '18 at 02:57