The awesome static factory pattern for Android.
Note: If you are looking for a “How-To” pass data between your Android components, I’ve written a small article explaining.
Starting a new activity in Android requires calling
MARKDOWN_HASH60e2cfa5aea562bf47291693fea059b7MARKDOWN_HASH, you must pass an Intent as a parameter the framework knows the desired action.
This intent can receive data (as extras) and the associated component class e.g. Activity.
Looking at the snippet, it’s easy to see the inherent coupling. If for some reason, the initial process/logic of MyActivitychanges and requires a change on each instantiation throughout the whole code base, you’re screwed.
Also if data needs to be passed both the created, and instantiating classes must have a reference to the same key(s) (string) to each intent’s extra, you may end up with a helper/static class with all these key values as a simple solution but, in reality, it’s just unnecessary boilerplate code to maintain for something as simple as changing activities.
Enter the Static Factory Pattern
Static factory methods are simple wrappers that encapsulate what you need to pass when instantiating a new component.
The above example (gist) shows how you can encapsulate your data in a static context (which means you can call this without having an actual instance) and use this method to instantiate MyActivity. As a bonus you could even make the MyActivity constructor(s) private, this will ensure the factory method is the only way to instantiate.
Now, whenever MyActivity needs to be instantiated just call the factory method like this:
Another cool tip I heard on the Fragmented Podcast (ep. 14) is throwing an exception on that private constructor, if for some odd reason one of your team-mates go around the factory method he will be informed that’s isn’t the intended way to instantiate 😉.
This pattern can be applied to other components, it isn’t specifically for Activities. In fact, the Android fragment has a newInstance() method that follows this same static factory pattern just type “newInstance” on Android Studio and it should pop-up as a suggestion.
That’s it, Happy coding!✌️
[AndroidBits]: Small articles (bits, get it? 🤓) discussing simple topics, that I found to be of immense value during my quest to become a better dev!
If you know someone who might extract value please share, I’ll be looking for your opinion and suggestions in the comments, feedback is always welcome.