Seasoned Android developers know to avoid keeping extra references to an Activity where possible, because if that reference outlives the Activity's normal lifecycle it can consume a lot of memory unnecessarily if the GC can't collect it. But it's easy to take this too far.
Theming info is part of a Context, and an Activity is a Context. Every View's constructor takes a Context as an argument, and LayoutInflaters provide that argument during inflation based on what Context you used to get the inflater. The theme information in the Context is used to initialize default styling for a View, which includes both appearance and behavior.
If you're in the habit of using your application context (from a call to getApplicationContext(), for example) in places where you need a Context to create views, it's only a matter of time until you find a case where things don't work quite like you would want or expect. But if the application context isn't the right one to use for constructing views, what is? It depends on where those views are going to end up.
Your Activity carries the theming info for your main Activity window, and should always be used when constructing or inflating Views that you intend to place there.
ActionBar has the getThemedContext() method, which always carries the correct theme for views that appear in the action bar. Use this when constructing custom views that will end up there. This is especially important if you're using a theme like Theme.Holo.Light.DarkActionBar that has a very different visual character from the rest of the activity content.
Dialog has the getContext() method, which will give you the theming you need for content in the dialog window. If you use AlertDialog.Builder, you won't necessarily have a Dialog object at the time when you need to inflate views, so we added a getContext() method to AlertDialog.Builder in API 11.
Finally, if you have a sub-pane of your UI with a very different visual character from the rest of the content, you can use this system to your advantage there too. Create a ContextThemeWrapper to decorate an existing Context with a different theme. Pass the ContextThemeWrapper to View constructors or to LayoutInflater.from() just like you would any other Context, and off you go. Any views you generate from that Context will have the default styling supplied by the theme you passed to ContextThemeWrapper's constructor.
All of this helps your Views automatically take on the character of the container they're placed in, and ultimately makes it easier to design and implement your app's internals.
#androiddev #androidprotip, always be mindful of Context when building your UI.
57 plus ones
Shared publicly•View activity
- Fantastic and timely, for me anyway, information. Thanks Adam.Mar 5, 2012
- Thank you for providing such a nice and very useful informationMar 5, 2012
- Could Google consider contributing to findbugs so users can use a static analysis tools to catch excessive use of context? The same goes for other bad practices mentioned in this space. Forgive me if there are already android plugins for static analysis tools.Mar 28, 2012
- So when in the life cycle does the application view get created?16w