This vs getContext vs getApplicationContext
Context
in Android is an interface to global information about and application
environment, it allows access to application-specific resources and
classes, as well as up-calls for application-level such as launching
activities, broadcasting and receiving intents, etc.
It’s the context of the current state of the application. It lets newly-created objects understand what is the actual state of the application.
Actions that involve Context are:
These invoking methods are used for different contexts, there are two main contexts in Android framework Application and Activity context, Application context is attached to the applications life-cycle and will always be the same throughout the life of the application, while the Activity context is attached to the Activity’s life-cycle an can be destroyed if “onDestroy()” is raised.
getContext()
Is used like View.getContext() and returns the context the view is currently running in.
getApplicationContext()
Is used like Activity.getApplicationContext() and returns the context for the entire application, used if you need a context tied to the lifecycle of the entire application, not just the current Activity.
getBaseContext()
Is used like ContextWrapper.getBaseContext() and Is use if you need to access to a Context from within another context.
And what about “this”, you could say that “this” and getContext() are the same, but they are not always the same, in an Activity class, you can use “this” because Activity inherits from Context, but method getContext() is not in Activity class.
In resume “this”, getBaseContext() and getContext() are activity contexts while getApplicationContext() is an application context
Here you can find the reference for Context in Android: https://developer.android.com/reference/android/content/Context.html , there you will find the associated methods and attributes that a Context has.
It’s the context of the current state of the application. It lets newly-created objects understand what is the actual state of the application.
Actions that involve Context are:
There are Different invoking methods by which you can get context, you can use:
- Loading a resource
- Launching a new activity
- Creating views
- Obtaining system service
or use the reserved word “this” when you are in the Activity class.
- getApplicationContext()
- getContext()
- getBaseContext()
These invoking methods are used for different contexts, there are two main contexts in Android framework Application and Activity context, Application context is attached to the applications life-cycle and will always be the same throughout the life of the application, while the Activity context is attached to the Activity’s life-cycle an can be destroyed if “onDestroy()” is raised.
getContext()
Is used like View.getContext() and returns the context the view is currently running in.
getApplicationContext()
Is used like Activity.getApplicationContext() and returns the context for the entire application, used if you need a context tied to the lifecycle of the entire application, not just the current Activity.
getBaseContext()
Is used like ContextWrapper.getBaseContext() and Is use if you need to access to a Context from within another context.
And what about “this”, you could say that “this” and getContext() are the same, but they are not always the same, in an Activity class, you can use “this” because Activity inherits from Context, but method getContext() is not in Activity class.
In resume “this”, getBaseContext() and getContext() are activity contexts while getApplicationContext() is an application context
Here you can find the reference for Context in Android: https://developer.android.com/reference/android/content/Context.html , there you will find the associated methods and attributes that a Context has.
No comments:
Post a Comment