Posts tagged context nullpointer
Android contexts and singletons
Feb 15th
Understanding 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:
- what is context in android (18)
- android context (4)
- how to initialize context in android (3)
- android keep activity alive (3)
- android singleton (3)
- android keep activity alive in background (2)
- android context when is it valid (2)
- context initialization andriid (1)
- get cuureny activity from application context in android (1)
- android what happens to singletons when an app closes (1)