Android Nightmares | Base classes

Android Nightmares 😱 | Base classesDon’t be that guyWhen I started working as Android Developer there weren’t Fragments, RecyclerViews, ViewModels, and Coroutines. Everything changed in these ten years, everything except one thing: Base classes🥶.If you…

Android Nightmares 😱 | Base classes

Don’t be that guy

When I started working as Android Developer there weren’t Fragments, RecyclerViews, ViewModels, and Coroutines. Everything changed in these ten years, everything except one thing: Base classes🥶.

If you never met a BaseActivity, BaseFragment or BaseViewModel consider yourself lucky🌟.

These classes are the wrong consequence of a correct principle in software development: Don’t Repeat Yourself

“Don’t repeat yourself” (DRY) is a principle of software development aimed at reducing repetition of software patterns,replacing it with abstractions or using data normalization to avoid redundancy. (Source)

A common scenario is when you need to trace analytics along your application, or maybe register a BroadcastReceiver on each activity.

It seems ok, now every Activity that extends BaseActivity will trace and logout without any new line of code.
But what if you need an Activity that automatically logs out without tracing logs? And what if you need an Activity that trace logs without an auto logout?

No, the answer is not Feature Flags🙄

Kotlin (even Java, with some additional line of codes) and Android Lifecycle components can save you from this nightmare with Delegation

Delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. (Source)

You can implement an interface and delegate the actual code to another class. Also, thanks to Lifecycle components, we can bind our logic to lifecycle.

You only need

  • Interface & Implementation for each feature (Single Responsibility principle 💓)
  • Your Activity/Fragment that implements the interface, delegates to Impl and attaches the lifecycle.

Final result:

Your delegates will look like these:

Have you ever used Delegation before? Tell me in the comments your worse experience with Base classes!😎


Android Nightmares😱 | Base classes was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Giuseppe Giacoppo | Sciencx (2024-03-29T13:18:47+00:00) » Android Nightmares | Base classes. Retrieved from https://www.scien.cx/2022/02/20/android-nightmares-base-classes/.
MLA
" » Android Nightmares | Base classes." Giuseppe Giacoppo | Sciencx - Sunday February 20, 2022, https://www.scien.cx/2022/02/20/android-nightmares-base-classes/
HARVARD
Giuseppe Giacoppo | Sciencx Sunday February 20, 2022 » Android Nightmares | Base classes., viewed 2024-03-29T13:18:47+00:00,<https://www.scien.cx/2022/02/20/android-nightmares-base-classes/>
VANCOUVER
Giuseppe Giacoppo | Sciencx - » Android Nightmares | Base classes. [Internet]. [Accessed 2024-03-29T13:18:47+00:00]. Available from: https://www.scien.cx/2022/02/20/android-nightmares-base-classes/
CHICAGO
" » Android Nightmares | Base classes." Giuseppe Giacoppo | Sciencx - Accessed 2024-03-29T13:18:47+00:00. https://www.scien.cx/2022/02/20/android-nightmares-base-classes/
IEEE
" » Android Nightmares | Base classes." Giuseppe Giacoppo | Sciencx [Online]. Available: https://www.scien.cx/2022/02/20/android-nightmares-base-classes/. [Accessed: 2024-03-29T13:18:47+00:00]
rf:citation
» Android Nightmares | Base classes | Giuseppe Giacoppo | Sciencx | https://www.scien.cx/2022/02/20/android-nightmares-base-classes/ | 2024-03-29T13:18:47+00:00
https://github.com/addpipe/simple-recorderjs-demo