Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use ContextImpl to implement Context rather than ContextWrapper in Android?

Tags:

android

Recently in the Context of two subclasses of the Context is not very understand, why use ContextImpl class to implement the Context, rather than directly using ContextWrapper?

like image 451
Red Rain Avatar asked Mar 11 '23 08:03

Red Rain


1 Answers

Context is the abstract class, which acts as an 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.
See Context class code here

ContextWrapper is an Adapter or proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context. [It uses adapter pattern]
See ContextWrapper class code here

ContextImpl is a common implementation of Context API, which provides the base context object for Activity and other application components.
See ContextImpl class code here

So if you browse through the code of above three classes, you can easily understand the abstract class Context class declares the API methods, ContextImpl implements those API methods, whereas ContextWrapper class is just an adapter of any Context type so it can be used at various places where adapter pattern is required rather than using Context child classes.

Hope it reduces confusion!!

like image 118
Shridutt Kothari Avatar answered Apr 27 '23 04:04

Shridutt Kothari