Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does LayoutInflater need a context?

Tags:

android

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 ?

like image 281
dev Avatar asked Mar 21 '23 19:03

dev


1 Answers

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.

like image 79
Raghav Sood Avatar answered Apr 09 '23 04:04

Raghav Sood