Merged
Conversation
ContextWrapper extends Context
baseContext of activity, service, application is ContextImpl instance
…troyed application context in activity is not released after activity destroyed
what is android context context and its subclasses application context VS activity context
* Common Pitfalls: Context Misuse * Conclusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Android Context
You will always encounter the Context class when developing an app.
Context is very important concept in android development.
Without Context, you cannot start an activity, broadcast, or service.
So understanding Context itself is helpful in understanding the components.
Various components(Activity, Service, Broadcast Receiver, Content Provider) are able to access
system services and application resources through Context.
What is Android Context?
Context provides information about the current state of the application.
Context is the interface that allows access to application-specific resources and system service,
and application environment.
The main features are:
Context and its subclasses
Contextis an abstract class.ContextWrapperandContextImplextendsContextclass. and ContextWrapper has a reference toContextImpl.Activity,Service,Applicationare concrete implementation ofContextWrapper.this diagram shows the relationship between
Context,ContextWrapper, andContextImpl.You can use multiple ways to use Context
There are multiple ways to get a Context instance.
For example, in Activity, you can use:
this(activity instance itself)getBaseContext()to get a ContextImpl instancegetApplicationContext()to get an Application instanceApplication Context VS Activity Context
Dialogs,Snackbar, Managing views and animationsUsing Application Context where Activity Context is required may cause unexpected behavior or
crashes.
Common Pitfalls: Context Misuse
Leaking Activity Context
Avoid holding a reference to a Activity Context in a static variable or singleton,
as this prevents the Activity from being garbage collected.
Example of Bad Practices:
Solution: Use Application Context instead:
Using Application Context for UI Tasks
Application Context cannot handle certain UI tasks, such as inflating layouts or displaying
dialogs.
Directly Instantiating Context
Context is a system-managed class and cannot be directly instantiated.
Using Incorrect Context for Views
Ensure the proper Context is passed when creating or inflating views.
Example of Bad Practice:
Solution:
Conclusion
Understanding and correctly using Context is vital for creating efficient and memory-safe Android
applications.
Choosing the right Context for the right task -
is a key to avoiding pitfalls like memory leaks and incorrect behavior.
Key Takeaways:
With proper Context usage, your Android applications will be more robust, efficient, and
maintainable.
Reference