Android Developer Roadmap — Part 2

The Android developer roadmap has been divided into five modules, each of which covers a different area of the Android development ecosystem. In the previous post, we covered key aspects of Android's architecture, such as the primary Android languages, the OS, the Android Platform, and the App Manifest.

In part two, we’ll cover the next three sections of our Android Roadmap:

  1. App Components
  2. Intents
  3. App Entry Points

Let’s get started!


App Components

image_2023_07_03T06_07_48_372Z

In Android development, app components act as entry points that let users and other systems communicate with your application. Each component is created and destroyed according to a specific function and lifecycle.

Now let us talk about each element:

Activities

An activity is an independent and reusable component that interacts with the user by providing UI-relevant resources. All Android applications must have at least one activity to enter the app and interact with users.

Activity Lifecycles

Each activity has its own lifecycle, which is an important concept in activity and resource management. The Activity class provides a basic set of callback methods that notify an activity when a lifecycle state changes.

The lifecycle sequence shown in the following figure will be used to determine when to call the callback method:

image_2023_07_03T06_08_05_854Z

With lifecycle callback methods, you can specify how your activity behaves and effectively manage your resources. In this section, we’ll discuss the six core callback methods below:

  • onCreate(): This callback is invoked when the system creates your activity. Most of the initialization logic, which should occur only once during an Activity’s lifespan, should be placed here (like creating views or binding data).
  • onStart(): This callback is invoked after calling the onCreate() method as the activity becomes visible to the user. This may happen more than once if you switch between multiple Activities or applications.
  • onResume(): This means the activity is ready to come to the foreground and interact with users.
  • onPause(): This means the activity is no longer in the foreground but may still be partially visible (for instance, if the user is in multi-window mode). In most cases, it indicates the user is leaving the activity, and the activity will enter the next state.
  • onStop(): This callback is invoked when the activity is no longer visible to the user. This may happen more than once if you switch between multiple Activities or applications.
  • onDestroy(): This callback is invoked before an activity is destroyed. The system invokes this callback when the activity is finished or the system is temporarily destroying the activity due to a configuration change. This callback can be utilized when you need to release or dismiss all remaining resources and allow the Garbage Collector to withdraw all allocated memories.

Creating an Activity

To create an activity, you must first create a class that is a subclass of the Activity class. To support compatibility with Themes, Fragments, and other components in modern Android development, the Jetpack library provides advanced Activity classes such as AppCompatActivity, FragmentActivity, and ComponentActivity. You can create a basic activity with the following code:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

    }

}
 

For your app to use activities, you must declare them on the App Manifest as seen in the following example:

<manifest ... >

  <application ... >

      <activity android:name=".MainActivity" />

      ...

  </application ... >

  ...

</manifest >
 

Services

A service is an entry point that is designed to perform functions for remote processes and to run longer-running operations in the background, such as a music player or YouTube video player.


Service Life Cycles

A service has its own dedicated lifecycle and provides two types of information that tells the system how to start and manage services:

  • startService: Another component can run a service by calling startService(). This service will run in the background, and another component can also stop the service by calling stopService().
  • bindService: Another component or a client can run a service by calling bindService(). The bindService() function provides an IBinder interface, which allows the client to communicate with the service consistently. This service will run in the background. Another component or client can also cancel the connection by calling unbindService.

As you can see in the figure below, a service’s life cycle depends on how it was created.

image_2023_07_03T06_08_16_120Z


According to the Android documentation, the figure above "distinguishes services created by startService() from those created by bindService()," but regardless of how services are started, they can potentially allow clients to bind to them.

Creating a Service

To create a service, you must create a class as a subclass of the Service class, as shown in the example below:

class MyService : Service() {

    private var binder: IBinder? = null

    override fun onCreate() {

        //The service is being created

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        return super.onStartCommand(

            intent,
            flags,
            startId

        ) //indicates how to behave if the service is killed

    }

    override fun onBind(intent: Intent?): IBinder? {

        // A client is binding to the service with bindService()

        return binder

    }

    override fun onDestroy() {

        //The service is no longer used and is being destroyed

    }

}
 

Next, for your app to be able to use the service, you must declare it on the App Manifest:

<manifest ... >

  <application ... >

      <service android:name=".MyService" />

      ...

  </application ... >

  ...

</manifest >


Broadcast Receiver

A broadcast receiver is a listener that can be set up to receive broadcast messages from the Android system and other Android apps. According to the Android documentation, broadcasts are used to send messages across apps and outside of the normal user flow, such as when the system boots up or the device starts charging.

Broadcast receivers, unlike activities and services, do not have dedicated lifecycles. Instead, until it is unregistered, it will listen to all assigned event messages.

Creating a Broadcast Receiver

To create a broadcast receiver, you must first create a class that is a subclass of the Broadcast Receiver class, as shown in the following example:

class MyBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {

        // do something

    }

}


Next, for your app to be able to use the service, you must declare it on the App Manifest:

<receiver android:name=".MyBroadcastReceiver"  android:exported="true">

    <intent-filter>

        <action android:name="android.intent.action.BOOT_COMPLETED"/>

        <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />

    </intent-filter>

</receiver>
 
 

Content Providers

A content provider determines how your application's data is accessed and shared with other applications. According to the Android Docs, a content provider enables your app to share any type of persistent data, whether it is stored in a file system, a SQLite database, a Jetpack Room, or on the web.

Content providers protect data by requiring specific permissions. If a requesting application lacks the necessary permissions, it cannot query the content provider's data.

image_2023_07_03T06_08_29_491Z


Conclusion

This concludes part two of the Android Developer Roadmap. This section covered the important parts of the App components and lifecycles, so you have a better grasp of how Android components and their lifecycles work.

Again, don’t be intimidated by the amount of information on this roadmap. Check out the sections that seem the most helpful to you to understand Android development. 

Varun V V