Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

BaseActivity and BaseFragment are monsters

 

 
Reza Bigdeli
Android developer https://www.linkedin.com/in/rezabigdeli/
Published: July 23, 2020
Tweet
Share
 

Are you a car? Are you a robot? Should I extend car with 4 wheels or should I extend humanoid robot with two legs?!?!?!?!?!?!?

 

An old tale of composition over inheritance, sort of!

One of the good practices when writing code is “Reusability”. Instead of writing the same code multiple times, we should write it once and use it wherever we need to. Simple as that!
Well not so fast! How can we achieve it though? The first thing that comes to mind is to find the similarities/duplications and put them in a base class. This way whoever that is extending our base class can have the functionalities out of the box without needing to write them over again! But there is a problem with this approach,. Our base class tends to get messy, creepy and hard to touch after sometime, specially in the languages that don’t allow extending from two or more classes.
A day to day normal example would be that of a BaseActivity. We tend to put a lot of logic in that class, things like layout inflation, analytics, activity/permission results, et cetera. We start off with something like:

 

 

Pretty clean, right? Well maybe now but not in a month. Specially when you introduced a ton of callbacks, implemented a lot of interfaces and added a big deal of logic! Your base activity is going to look eventually like:

 

If you don’t want to read this code, you’re not alone!

 

You see what I mean? Have fun extending this class, let alone refactoring or bug fixing it! No need to mention migrating to new technologies is going to be extra troublesome if your base activity is dependent on old tech.

OK, so how can we go about fixing it?

 

Layout inflation

If you are only worried about layout inflation for your activities and don’t want to repeat setContentView() all the time, then simply use AppCompatActivity(@LayoutRes contentLayoutId: Int) constructor from androidx.activity:activity-ktx:1.1.0 and for fragments Fragment(@LayoutRes contentLayoutId: Int)constructor fromandroidx.fragment:fragment-ktx:1.2.0.

 

 

Setting up toolbar

To setup toolbar just use extension functions on activities and/or fragments.

 

And then call this from your child Activities with toolbar you want!

 

This way only the activities who actively want to setup a toolbar would use it! And if some activities have a particular way of setting up their toolbar, then they can use another method or just write it their own way!

 

Activity/Permission results

This shouldn’t be something that make people use base classes but if you are using a base class to handle these stuff, maybe take a look at ActivityResultContract in androidx.activity:activity:1.2.0-alpha04

 

Analytics

To send tracks and do analytics stuff based on activity/fragment callbacks you can just use ActivityLifecycleCallbacks in your Application class for activities and for fragments you can use FragmentLifecycleCallbacks


When to use Inheritance then?

As discussed, when it comes to addressing duplications and code reusability, it seems that Inheritance isn’t the right approach. Instead we should try to use functions or compose smaller classes!
But if we are trying to bring about a new framework by creating a new concept of an activity and adding some new functionality and properties that are local and native to the new concept, then Inheritance is the right approach. And I wouldn’t suggest BaseActivity as its name! Maybe something like MyFrameworkActivity.

We should also acknowledge that there isn’t a fine line that determines when to use what approach!


TL;DR

To make your code reusable don’t try putting everything in a giant base activity or fragment (inheritance), instead introduce composition with the help of extension functions or dividing logic into different classes. Because:

  1. It makes it easy to migrate to new tech/architecture.
  2. It makes it easy to start writing new activities and fragments, that are well know by all developers, instead of making them understand your version of activities and fragments!
  3. Plus all the benefits composition brings over inheritance!

TBH, I couldn’t find a use case or a good reason for using base activities or base fragments. Let me know if you know of a good one!

 

 

Tags: Android, Android App Development, Oop

 

View original article at: 


 

Originally published: July 03, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One