Just ironed out a bug in my Android app. I was trying to use getAssets() to pull a file from my assets directory. I subclassed Application and returned a “getApplicationContext” object so that all my classes can use a context whenever they need to.
But after much headache and NullPointerExceptions, it turns out that I needed to pass a local context variable and use THAT instead. If I use a global application context, getAssets doesn’t work!
So why is this? What’s so special about a local context variable that makes it work. I thought any old “Context” variable was enough to access the necessary methods and make them work properly!
A local context variable is also an Activity. The Activity provides extra functionality that the AssetManager, you call using getAssets(), uses. Android strictly uses contexts and not activities in order to avoid memory leaks. You should almost always use your Activity’s context.