This post introduces the concept of a utility class and gives a simple example of how you can use one to tidy up your code. As Android projects grow in size, it becomes increasingly important that your code remains organized and well-structured. Providing a utility class for commonly called methods can help tremendously in reducing the complexity of your project, allowing you to structure your code in a readable and easily understandable way.
Note: please read this short post before continuing forward.
A common issue in Android development is backwards compatibility. How can we add cool new features from the most recent Android API while still ensuring that it runs correctly on devices running older versions of Android? This post discusses the problem by means of a simple example, and proposes a scalable, well-designed solution.
As with most areas in software engineering, debugging is a crucial aspect of Android development. Properly setting up your application for debugging can save you hours of work and frustration. Unfortunately, in my experience not many beginners learn how to properly make use of the utility classes provided in the Android SDK. Unless you are an experienced developer, it is my personal belief that Android debugging should follow a pattern. This will prove beneficial for a couple reasons:
With Android 3.0 came the introduction of the
LoaderManager class, an abstract
class associated with an
Fragment for managing one or
Loader instances. The
LoaderManager class is one of my favorite
additions to the Android framework for a number of reasons, but mostly because it significantly
reduces code complexity and makes your application run a lot smoother. Implementing data loaders
LoaderManager is simple to implement, and takes care of everything about
lifecycle management so are much less error prone.
I recently came across an interesting question on StackOverflow regarding Fragment instantiation:
What is the difference between
MyFragment.newInstance()? Should I prefer one over the other?
Good question. The answer, as the title of this blog suggests, is a matter of proper design. In this
newInstance() method is a “static factory method,” allowing us to initialize and setup a
Fragment without having to call its constructor and additional setter methods. Providing static
factory methods for your fragments is good practice because it encapsulates and abstracts the steps
required to setup the object from the client. For example, consider the following code: