Bob, Thanks for this and other posts. If it weren't for yourself, Mark Murphy and TreKing I would be pulling my hair out - and its getting too precious to be doing that these days.
As someone with not a lot of experience in things Android, the Context is one of the most confusing things to me and I would love to see a blog article/ Mark Murphy chapter specifically on each of the various levels of Context returned by the various methods and when they should be used. Frankly there are that many posts about:- * dialogs and conflicts with documentation like this one. * toasts and leaks ( http://groups.google.com/group/android-developers/browse_thread/thread/de2f0cebec002303/475f545e947a5915?lnk=gst&q=toast+leak+context#475f545e947a5915) * leaks and using ApplicationContext with long lived objects "There are two easy ways to avoid context-related memory leaks. The most obvious one is to avoid escaping the context outside of its own scope. The example above showed the case of a static reference but inner classes and their implicit reference to the outer class can be equally dangerous. The second solution is to use the Application context. This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication()." - http://developer.android.com/resources/articles/avoiding-memory-leaks.html * Binder.java having internal comments warning about potential memory leaks using non-static sub-classes - but nothing in the javadoc documentation. (Well, maybe experienced Java developers are fully aware of the references inner classes have on their outer class - but some of us have very little experience and are putting our apps alongside yours) THAT quite frankly confuses the hell out of anyone new to Android. There doesn't appear to be any real documentation in the developer Resources area (I apologise if I have missed it) that really explains what all the different hierarchies of context are, what they give you or with what types of objects they are to be used. Hopefully I am correct in saying that every Intent is either passed or receives from somewhere a Context of some form - but there is little information on what that is or should be. I'll be the first to admit that, when I first started and until quite recently, been passing contexts that are as non-specific or higher up in the hierarchy as possible with the perhaps misguided opinion that - " well at least if I leak this it wont keep around anything else that's not there anyway". Yes, I agree with you I am an idiot who doesn't know the first thing about leaks, SoftReferences and memory management. But until I started learning about these things and MAT and tracking leaks, without a real resource on Contexts - can you forgive me for succumbing to temptation? After all, until you know better isn't Java supposed to take care of all this for you? With the ease of publishing apps on the market, goods apps put out there by people like yourselves - are next to apps by teenagers with little experience in Java let alone Android. Bad apps impact the good ones, not just whilst they are running, but also through the entire "Android experience" that users get. If an app force closes one gets this little voice in the back of their head telling them that Android, and not just that app, is prone to being buggy. When most of the time it is newbies like me who fail to really understand the Android environment whinging and complaining about Androids faults when all we are really doing is externalising our own shortcomings. On the whole, I love Android, but an article on Context best practice (or maybe half a chapter, or Appendix table, in one of Mark Murphys books) is really needed so that Learner drivers like myself don't ruin the Android experience for everyone. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en