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
Compose CameraX on Android
Compose CameraX on Android

By Peng Jiang

Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.

By ProAndroidDev -
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code With Kotlin Scopes
Noisy Code With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

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