google android context and singleton class and application relaunchUnderstanding contexts in android correctly is every developers nightmare. This article highlights some of the findings which will help you understand contexts in android a little better. This article is more useful if you follow a singleton pattern in your app.

The general rule of thumb when using contexts is to use the activity’s context whenever possible. I would like to highlight some of the issues when I followed this rule blindly without understanding contexts correctly. In one of the projects, a lot of singleton classes were used to maintain states of different components used throughout the app. For e.g, DatabaseManager was a singleton which was a single point of entry to perform all the DB operations. Now, there are lot of developers who hate using singletons in java, because it gets abused quite a lot and make is difficult to reuse/separate components if required. Read this for more info.

Anyways, we ended up using a singleton pattern throughout our app. But some of the singletons required to use the android context for initialization. So the first thing which came to our mind was to initialize these singletons on app startup.

Mistake 1

The first mistake we did was we used Continue reading “Android contexts and singletons” »

Incoming search terms: