dev
dev

Reputation: 11309

Why does LayoutInflater need a context?

Just wondering on why the implementors decided for the developers to pass the context (even though the system services seem more like a singleton for the developers, and we mostly don't even care):

LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(layout);

I am asking this more because of an implicit fear of leaking memory/context whenever I play with context. Is there a possibility of mis-handling context here ?

Upvotes: 5

Views: 633

Answers (3)

Bharat Sharma
Bharat Sharma

Reputation: 3966

As per android documentation:

Context: 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.

So "context" holds different environment parameter. These parameters are very much application and system dependents.

"why the implementors decided for the developers to pass the context"

When you creates any activity or creates any dialog or service or anything then it needs several different environment properties in Android. So there should be something which can provides all these information and can perform different task internally according to your operation. In Android, context does it.

Example: If you send Broadcast in Android then it will be received by all broadcast receiver in different activities and application also. These things you does not manages but you can use, everything is already managed by Android that is the reason we mostly don't even care.

Context is provided by system So we don't really need to take much care of memory leaks. If you will look different public parameters(http://developer.android.com/reference/android/content/Context.html ) then you will find that most of the things you can set or simply get. Functionality also are at application level not much system levels. So leaking you need to take care for you code not actually for context.

Upvotes: 1

Raghav Sood
Raghav Sood

Reputation: 82543

In Android, your app's Context is essentially like a pipe connecting it to the system services. A lot of the system services are singletons, but you cannot arbitrarily access them. The Context class acts as a middleman to receive and pass the service you need to you.

LayoutInflater.from(context); simply goes and calls context.getSystemService() using the supplied context, which is your application's.

In essence, you app and Android are two separate things running simultaneously and Context adds as a pipe to connect them.

Upvotes: 6

nurisezgin
nurisezgin

Reputation: 1570

Because every android service needs context. LayoutInflater is android service. Another declaretion about this;

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Upvotes: 1

Related Questions