iamjonfry.com

Android - Creating a global Application Context

May 17, 2014

Proper use of the Android context seems to be tricky to get right. A common problem is passing the context around unnecessarily.

An example of this, is when you are reading or writing files to disk, as this requires the context to do so. However, you do not want to pass the context into the method or class which you are using, so you can instead use a simple trick to gain access to the Application Context from anywhere.

To do this requires you to subclass the Application class.

You also need to declare this in the application tag of your Android Manifest file

<application android:name=".MyApplication"
       android:icon="@drawable/icon"
       android:label="@string/app_name">

Jonathon Fry

Written by Jonathon Fry

TwitterGithub

iamjonfry.com © 2018