{"id":6994,"date":"2014-05-16T01:20:19","date_gmt":"2014-05-16T01:20:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/singletons-vs-application-context-in-android-collection-of-common-programming-errors\/"},"modified":"2014-05-16T01:20:19","modified_gmt":"2014-05-16T01:20:19","slug":"singletons-vs-application-context-in-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/singletons-vs-application-context-in-android-collection-of-common-programming-errors\/","title":{"rendered":"Singletons vs. Application Context in Android?-Collection of common programming errors"},"content":{"rendered":"<p>I very much disagree with Dianne Hackborn (and yes, I realize she&#8217;s an Android framework engineer, thanks.) We are bit by bit removing all singletons from our project in favor of lightweight, task scoped objects which can easiliy be re-created when you actually need them.<\/p>\n<p>Singletons are a nightmare for testing and, if lazily initialized, will introduce &#8220;state indeterminism&#8221; with subtle side effects (which may suddenly surface when moving calls to getInstance() from one scope to another). Visibility has been mentioned as another problem, and since singletons imply &#8220;global&#8221; (= random) access to shared state, subtle bugs may arise when not properly synchronized in concurrent applications.<\/p>\n<p>I consider it an anti-pattern, it&#8217;s a bad object-oriented style often embraced by people coming from procedural languages like C, where it is common to maintain global state.<\/p>\n<p>To come back to your question: Although the app context can be considered a singleton itself, it is framework-managed and has a well defined life-cycle, scope, and access path. Hence I believe that if you do need to manage app-global state, it should go here, nowhere else. For anything else, rethink if you <em>really<\/em> need a singleton object, or if it would also be possible to rewrite your singleton class to instead instantiate small, short-lived objects that perform the task at hand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I very much disagree with Dianne Hackborn (and yes, I realize she&#8217;s an Android framework engineer, thanks.) We are bit by bit removing all singletons from our project in favor of lightweight, task scoped objects which can easiliy be re-created when you actually need them. Singletons are a nightmare for testing and, if lazily initialized, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6994","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6994"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6994\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}