On fragments and life-cycles

“When should we trigger dependency injection in fragments?” Was a question my colleague asked me. “Well why not just in Fragment#onCreate()?”, I replied. However, my colleague pointed out that apparantly Fragment#onCreate() is not always called at the same time with relation to Activity#onCreate(). This is important for us as we initialize a Dagger component in the Activity and use that in the Fragment. We could of course just blindly follow the advise on the internet and solve this problem by doing whatever we want to do in Fragment#onActivityCreated(), but I wanted to know why the life-cycle methods where not always called in the same order.

For our purposes let’s consider the following piece of code:

public class MyActivity extends FragmentActivity {
  protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
      Fragment f = new MyFragment();
      FragmentTransaction t = getFragmentManager().beginTransaction();
      t.add(R.id.fragment_container, f);

We will analyze what happens when we first launch MyActivity and what happens later when it gets re-created. We will look at when the life-cycle methods are called and why they are called at that specific time. Hopefully this will give you a better understanding of the inner workings of fragments.

Continue reading

Org-mode after one month

I set out to try org-mode for one month, with the intention of it replacing my paper notebook. On paper I use the bullet journal technique which works fine for taking notes as you go and making sure I don’t forget stuff. But I wasn’t 100% happy with it. It was hard to organize project related notes. The physical limitations of paper where just getting in the way, hence my endeavor into org-mode.

Initially I tried to replicate my exact work-flow on paper inside org-mode. After two weeks of trying it just wouldn’t stick. The bullet journal is great on paper but a disaster digitally (at least for me).

Right now I use org-mode, much more as a wiki. I have an index.org file, which serves as a landing page and it has a bunch of links to other org files that I can follow. It has a list with current projects (a file per project) and a link to a ideas for future projects. It also has a section for (private) journal entries and a section for blog post drafts. Previously I setup Jekyll with support for org-mode files, so I author my blog posts in org-mode in my wiki like environment and once I am happy, I just copy the file to my Jekyll _posts folder and done.

This solves my project based notes problem that initially wanted solved, but I still haven’t found a work-flow that sticks for taking notes on the fly that are one of tasks that don’t belong in a project. I have to spend some more time with org-capture to figure out a work-flow that works for me. I do struggle with bringing my laptop to meetings a bit. I find that people who bring a laptop to meetings are (or at least seem) less invested in the meeting and are easily distracted. For that I might go back to my paper notebook for taking notes during meetings and processing those into my org-mode structure afterwards.

Maybe I will turn around once I have spend more time with org-capture. Stay tuned for more on the topic.

Continue reading

Discovered Emacs org-mode

I never really to the time to look into Emacs. I was always a happy Vim user, loved the modal editing and because Emacs didn’t have that I never thought it could make my life that much better.

But then I learned about org-mode… What a sophisticated piece of software…

I currently keep a paper notebook and use a subset of the bullet journal, but have also tried various cloud note keeping tools. I never liked those cloud offerings as it is to much of a lock in of my data. I like my data to be private and hack able. Previously it was not always the case that I had a laptop with me, it still isn’t, but it is become more and more common to bring your laptop/tablet to meetings and I carry my laptop around a lot more than previous.

What really appeals to me about org-mode is the fact that you can combine free form notes with todo items. Most textual todo applications like todotxt can only manage todo’s, where as in my notebook I can combine todo’s with notes and journaling. org-mode seems to get this as well and might be the closest text based thing to a physical notebook.

Reading about org-mode has convinced me to spend some time learning Emacs and learning org-mode. I will trial it for the month of December to see how it goes and if it will replace my paper notebook.

My biggest challenge will be learning Emacs key-bindings. I am a bit on the fence about throwing evil-mode at the problem. It will jump start my learning, but I am worried it will prevent me from truly learning Emacs. Would love to hear some thoughts of somebody using evil-mode.

Continue reading

On Android fragmentation

As an Android developer I get asked a lot about fragmentation on Android and if it’s doable in dealing with it. Judging from the attention it gets in the media, the question is understandable. There is supposedly a lot to do about Android fragmentation. Every once in a while stories about the subject dominate the news. But as an software developer and every day Android developer the question always surprises me a bit.

Nothing new here

Before I was into Android development, I build websites and web-applications. And I don’t recall people asking me about how to deal with all the screen sizes and all the different browser versions. There was IE6 of course, but somehow everybody had some sort of understanding that IE6 was the only exception.

Similar is the situation when developing applications for the desktop. Different versions of the OS, different screen sizes, different hardware. Nobody seems to wonder how the developers of MS Office have to cope with it all. I my opinion fragmentation is nothing new. It’s more unique that you do not have to worry about it (like on iOS in a way).

So how does fragmentation manifest itself on Android and is it really a that bad?

Screen sizes

This is the obvious one. It might look like a big problem, but I think it actually isn’t. First of all because Android gives you a lot of tools to deal with different screen sizes. Think of the densities (LDPI, MDPI, HDPI, etc..) and the layouts you can override .

Second when you specify all your dimensions in dp’s (density independent pixels) you will discover that most screens have roughly the same aspect ratio. Now you just test the on the smallest screen you want to support (say HTC Wildfire) and on the biggest screen you want to support (Samsung Galaxy S III). If it looks good on both you’re done, if not tweak a bit (mostly for the small screen).

What about tablets? Most clients I worked with in Europe are not yet paying for a tablet layout, but if you have to build a hybrid application, than the layout variants are your friend. There are a lot of guides on the http://developer.android.com that explain to you how you can build an application that adapts to the users available screen size .

Different versions of Android (API levels)

This is also a problem on iOS, but on Android it’s bigger because vendors update slowly or not at all to new versions of Android. Therefore there are more different versions of Android available in the wild than versions of iOS.

In my opinion this is the biggest problem with Android fragmentation. In practice I’m still writing apps for Android 2.2 and judging from the market share it is going to take a while before we can leave Android 2.X behind. 2.3.X has a whopping 54% market share. This means it is gonna take at least 3 more years before we can even think about developing for Android 4.X only. This is sad as the Android 4 SDK brings a lot of good new stuff, but the best stuff is backported by the support library or efforts like Actionbar Sherlock . So it is manageable.

Vendor specific bugs

This is a real problem but fortunately also very rare in my experience. I have encountered this problem only once. It was an old HTC phone running Android 2.2 and it didn’t want to parse a date while the date format was exactly as specified.

The team had a really though time finding the bug, but that was because the exception got consumed by a catch clause (which wasn’t intended to catch it). Working around such bugs has been easy but detecting them can be a pain if disregard best practices. In other words never write catch (Exception e) {}, it is evil.


This has become a pretty long post. This may lead you to believe that fragmentation is a big problem, as it seems you have to account for a lot of things. I am certainly not saying that the fragmentation does not exist and you certainly have deal with it. But that said, when you stick to the best practices and build your app “the right way™” I find it doesn’t take a lot of extra effort take make your app run on all the devices and scale across all the screen sizes.

Continue reading

Virtualizing my development environment

Last week I have been busy moving my development environment from my laptop to a virtual server running on my laptop. This my seem like a hassle and the advantages are not all obvious, so I would like to share my findings and the specifics of my setup that I settled on.

Security / Privacy

First I would like to point out the problems I had with my current setup.The most urgent concern I had was that if my laptop would get stolen all my projects would be out in the open. I use the laptop primarily for my company projects and thus contains at lot of information from my clients. Think about copies of a database with maybe lots of user account information. It would be bad if my company would be responsible for the leaking of private information of other company’s. It is clear that I needed a way to protect my information (probably encryption), but I didn’t want it to get to much in my way.

It should just work

My primary OS is Archlinux. As a desktop OS it suits my needs just fine. I like to tweak things and it runs very fast. On the other hand I am very used to Ubuntu and typically setting up a LAMP environment is less work on Ubuntu that it is on Archlinux. It also matches better with the servers we deploy on and the other developers are also Ubuntu users.

Most important my development environment should just work ALWAYS! I’ve had some occasions that I reinstalled my system and forgot to install some tools only to discover at an appointment with a client. Not good… Isolating the development environment from my host OS enables me to chose a different environment and also allows me to preserve it during reinstalls of the host OS.

The setup

Enough blabla, on with the details. For virtualization I used Virtualbox. I haven’t used anything else and directly admit I use it for its simplicity. As a guest OS I use Ubuntu Server. I installed it with encrypted LVM. This way all data inside the virtual server is encrypted (and thus sort of safe). This safes me from all the trouble of keeping track where my database is stored and where my documentroot is from the webserver and if they are exposed or not.


So far all has been nice. But I can’t use my virtual server without access to it. There are a number of options to try and I will discuss 2 possible setups.

First I went for bridged networking. This way the virtual server will appear in the same network as the host. This is quite flexible and only has two problems. The first being that it is not always good to have your virtual server accessible for the hole network (for instance when connecting directly to the internet or hotel wireless). Second is more of a bug and that is that it just not always works on all networks. Some routers just don’t seem to get it when two operating systems request an ip from one physical network card.

To solve the to issues a made a second setup. This time I configure two network cards on the virtual server. One uses NAT and is primarily for internet access. The second uses host-only networking and is used by myself for accessing the virtual server. This has some advantages. First being that it works on buggy routers and second, it makes the network layout much more predictable as you have your own internal network address space. This allows you to hard code the network addresses in scripts. The only disadvantage of this setup is that other computers can’t access the server.

At the moment I use the second solution. Maybe I will switch the NAT to bridged networking when the routers allow it, but I’m not sure if I will need it. The host only network is great and saves me the trouble of looking up the ip from my server every time. I can now use a script to mount the document root using NFS.

Hope this was useful comments are welcome.


Continue reading