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.
- EnercomAsesor Área De Operaciones, 2013 - present
- Amadeus IT GroupIntern, 2012 - 2013
- EnercomEstudiante en práctica, 2011 - 2011
- Universidad de ChileIngeniería Civil Industrial, 2008 - present
- Ecole Centrale ParisIngénieur Centralien, 2010 - 2013
Massive Google Search 3.1.8 Update Rolling Out, Brings Google Experience...
A huge Google Search update started rolling out earlier today. It is absolutely massive on many levels: It officially brings Search version
[Holy Shit] Motorola Announces Project Ara, An Open, Modular Smartphone ...
Remember Phonebloks and how we all had a good laugh about how unworkable and idealistic it was? It turns out Motorola has been working on a
biobiochile - Trabajadores de Adimark desconfían de resultados de última...
biobiochile - Trabajadores de Adimark desconfían de resultados de última encuesta
Use a Math Formula to Increase Your Edge in Negotiation
Negotiation skills are valuable in multiple situations, from haggling over the price of a car to trying to get a raise. Writer and investor
YouTube - Abuso policial en Chile - 28.10.2011 ante la CIDH
Create AccountSign In. Home. BrowseUpload. Hey there, this is not a commercial interruption. You're using an outdated browser, which You