Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android require a Context to access a resource? [closed]

Tags:

android

I get why Android requires you to pass a Context object when trying to get a view for example, show a Toast, etc.

However, I don't really get the point for requiring it to access resources that could be shared by different Contexts in your application.

I several times found myself trying to access a resource from a utility class using a static method or something along those lines, and find it pretty annoying to require a passing of a Context param.

I fail to see where something could go south with this.

like image 460
Mateo Avatar asked Nov 27 '14 12:11

Mateo


1 Answers

From the official documentation :

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

In clear, the context is the middleman between the telephone resource, and your code. And it seems logical, that you cannot access to this from everywhere.

The reason you can only access to Context in Activity and Application classes, is because both derives from Context :

java.lang.Object

↳ android.content.Context

  ↳ android.content.ContextWrapper

      ↳ android.view.ContextThemeWrapper

          ↳ android.app.Activity
like image 72
FR073N Avatar answered Oct 11 '22 01:10

FR073N