Building blocks of Android | Part 1

Jun 17, 2021·

4 min read

Building blocks of Android | Part 1

Understanding fundamentals is important so that you can come up with a solution to any problem.

Hi, I am Sanjay and I am going to show you some of the fundamentals that every android developer should know.

In Android, everything you create will majorly come under 4 categories:
- Activities
- Services
- Broadcast receivers
- Content providers

These are the main app components and each component has its own purpose. Some may depend on other components. Each app component must be registered in the AndroidManifest.xml file.

Let's break down each component.

Activities:

You must be familiar with Activities since it is the most basic component every app should have.

Maybe some apps don’t have, I don’t know. If you got anything comment down so everyone can get to know : )

Activity provides an entry point to your app. Did you know how this thing got this name “Activity” ???? Because it provides an interactive UI where a user can perform tasks with only one Activity (Task) at any instant.

For example, take a chat app, the list of contacts screen is one activity and the message screen is another Activity. You can’t open both screens simultaneously.
Don’t argue with me about split-screen XD;

One app can have multiple activities and call other app’s activities (If the app allows).

Here comes the concept of Intents.

If one process starts one Activity with its name it’s called Explicit Intent.
Like the Gmail app opens Compose Screen when the user clicks the compose button. It actually specifying the Compose Screen name inside the code.

If a process starts an Activity without specifying its actual name but specifying the type of task wants to do, it is called Implicit intent.
Like the File Manager app, when we click any image file it opens the default Photo gallery app. The file manager app doesn’t know about the gallery app. It just wants a page that can handle image files.

Enough theory….. Let's see the code.

Steps to create an Activity:

Create a xml file for the UI inside the res > layout folder with name activity_main.xml

Create a Kotlin class in java > your_package_name folder.

Each Activity class you create must extend the Activity class. But for Backward Compatibility support, we should extend from AppCompatActivity from the AndroidX AppCompat library.

Then override the onCreate method where you set up a view to show to the user.
It may take time to sync with me, please be patient. I’ll show the code so you will get to know.

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  }
}

Here I used setContentView to inflate an XML file to generate UI.

setContentView will actually render the UI onto the screen. Without this function call, there will no UI, just a white screen.

You have created an Activity, WOW!! Congrats…

But wait…. there’s always a gotchas waiting for Android Beginner ; )You have just created a class, that’s it.

Android system won’t know about your class. We have to declare it on the AndroidManifest.xml file…

Woohhh!!! So much of work, right? Programmers are always lazy like me…..

Remember? When you create a New Android Studio Project, you get a MainActivity and activity_main by default. It does all the work for you.

To create new activity, go to File > New > Activity > Empty Activity.

That’s it. No creating classes, declaring it in Manifest file.

Services

Services are somewhat similar to Activity except it don’t have a UI. It is actually used to perform long-running tasks even after the apps is closed. There are some restrictions in the usage of Services in the latest Android releases. In most cases we don’t use Services instead we use WorkManager for the long-running tasks.

Don’t think that work done in Services is in a different thread. NOOOO….
Services actually run in the UI thread. So that if you have a function that consumes high CPU it might freeze your screen and the system will show ANR dialog (App Not Responding) screens. It will affect the User Experience.

So what is the solution?? We can do that work in a different thread. SIMPLE…. : )

There are many types of services you can implement.

But you are living in a Modern times right? Why creating services, creating threads……. Android gives you different approaches to achieve this usecases.

WorkManager 🥁🥁

Checkout this doc for WorkManager

But if you want to learn Services, check here

Broadcast Receivers and Content Providers will be covered in Part 2.

Source: Android Docs Guides

See you there soon …. Happy coding : )