Quick, and very dirty, Android Development Trick

In my main Activity (or any of them for that matter) I always include the following line:

public static volatile Context ctx;

Then, when the Activity is created (in onCreate), I set it to the following:

ctx = this.getApplicationContext();

This does three things.

1) It means I have easy access to a context pointer that won’t leak (if the device rotates or the original Activity is destroyed)
2) It makes anyone who knows what they’re doing die a little inside when they look at my code.
3) It means I don’t have to lug around a context pointer as a parameter whenever I need one.  I just make a call to “MyActivity.ctx”


One Response to “Quick, and very dirty, Android Development Trick”

Leave a Reply