Things I Wish I Knew When I Started Android Development

May 3, 2015 - 1 minute read -
android

Views over Fragments - Fragments are good in theory, but not in practice. As explained here Fragment transition state errors are notoriously difficult to debug. Views are simpler with fewer lifecycle methods; anything you want to use a Fragment for can be done with Views instead, because Views have been around since the beginning (Fragments were added later). A few components are easier with Fragments, like DialogFragments and ViewPager, only because the default implementations take care of UI interactions smoothly.

Crash fast: Square’s approach to Android crashes

Singletons - The Singleton pattern is everywhere in the Android framework. Any libraries you use will likely keep around a Singleton instance. They’re super useful. Here’s an implementation example.

Careful using Async Tasks - Sometimes you need long-running operations to be done in the background, so as not to block the UI thread. Just make sure that you gracefully handle the cases when the task returns and the calling Activity doesn’t exist anymore, or the app has backgrounded, or when the task just fails (perhaps because it lost internet connection). Finally blocks are useful in these cases.

Memory Leaks -Try not to pass around Contexts. When you have to, make sure references to the Context aren’t kept around; otherwise, it can’t be garbage collected. Here’s a talk on common memory leak pitfalls, and using memory analyzer tools to debug memory leaks.

Edit July 4th - LeakCanary is amazing.