About two weeks ago I was contacted by Sophie Hunt from Comparitech. To be honest I had never heard from that company and so I actually overlooked her first mail in my Spam folder (my apologies). Thankfully, Sophie sent another mail and I realized that for the first time someone actually contacted me because of my blog. My old blog that is, which I now use for personal things. More specifically, she referred to a post from 2012.
So I ran face first into a wall once more. Metaphorically, when coding, I mean. Well, I literally ran into a wall way back when, too, but this time I’m talking about a coding experience… I haven’t decided what’s worse yet.
Anyway, so it all began when I had to download some configuration files in an Android app. This was only a very minor task in the context of said app and I should probably also add that I don’t yet have as much experience in Android coding as I have in iOS programming. I know there are several libraries that provide extensive functionality for making all kinds of HTTP requests and such, but in the end I foolishly decided against including one in the app for such a minor task. Mind you, the idea was very simple: The user clicks a button, and a single, tiny JSON file was downloaded via HTTPS.
Context (or “What brought this on?“)
A couple of months ago I had to create a lazily initialized singleton class for Android, a logger. If you’re now thinking “Gero, no! Singleton is an anti pattern, you shouldn’t use that!” I’m afraid I have to inform you that you know nothing, Jon Snow. But I’m not gonna be an Ygritte, so for you I’ll add some special explanations in a future post. (Also I’m not going to sleep with you. You’re welcome.)
The logger needed to include some app specific information to each logged string, for example the app’s version number. For this you usually need to access methods from the
Context class on Android. Every
Activity is also a
Context, so it was no problem to get that. There was one problem, however, and that has to do with the way Android handles those Activities. For the un-initiated: An
Activity is basically responsible for the UI, usually a (part of a) screen the user sees. However, these objects are not persistent. For example, if you rotate the device, the
Activity displaying your current screen gets destroyed and rebuilt, so the changed layout is properly recreated to fit the new screen dimensions.