Android Base Classes From Scratch

Ayush Shrivastava
2 min readAug 15, 2020

Developing Android Apps has always been an interesting thing, starting from drawing the UI to writing its logic it has a different type of joy to see your app do things.

But wait! Does it look like this?

Codes like this will work for sure but reduce the readability and in case I have a common function for multiple activities don’t you think that its a weed break thing?😒

I believe that a real programmer is someone who knows how to hide feelings 😂 and we have to implement that skill on our apps as well using Inheritance.

To overcome such a heavy task, there’s a solution i.e. BASE CLASSES

Understand this, that we have a lot of common things in every classes which can be reduced and certainly help us do less work and is more efficient.

We all see onCreate() available in every activity’s class. Don’t we? It inflates the view and sets up the widgets, let’s wrap things into a BASE class

Now, this is how we can reduce things, This abstract class does all the things of an onCreate(), hence I don’t have to write it in every class anymore. I just have to implement this base class to different activity’s classes like this:

This was just an example of how easy things can become if we make BASE class. There could be any number of common methods depending upon your project which can be kept here eg. checkInternetConnection() etc.

The best part of having this is we can have base classes for everything we want. I generally keep 3 base classes BaseActivity, BaseFragment, and BaseViewModel, depending upon my project.

Thanks for reading.. :D

--

--

Ayush Shrivastava

📱Building Android Apps | Kotlin | Java | Open Source Enthusiast | Startup Enthusiast