Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to Android App Development With Kotlin: Activity and its Lifecycle (Part 2)

DZone 's Guide to

Introduction to Android App Development With Kotlin: Activity and its Lifecycle (Part 2)

Learn everything you need to know about Activity and its lifecycle in Android Studio.

· Java Zone ·
Free Resource

This is the second installment of the Introduction to Android App Development with Kotlin. In the previous part, we learned how to set up Android Studio and run our first project. In this part, we’ll learn more about:

  • Activities
  • Manifest files
  • App resources
  • And we’ll briefly discuss the project structure.

Activity

The Activity class is a key component of an Android app. It provides the window in which the app draws its UI (User Interface). Here’s an example activity class that came with our starter project

class MainActivity : AppCompatActivity() {

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


It’s a simple class that extends the base class AppCompatActivity. Every Activity has a lifecycle, known as Activity Lifecycle, that it follows very strictly. Please review and familiarize yourself with the diagram below.

Android Activity Lifecycle Diagram

As you can see, the diagram begins with the Activity being launched. Then, it goes through a bunch of different states represented as grey boxes. Each one of those states is a method that you can override in your Activity. If you are overriding one of those methods, make sure to call the parent method using the super  keyword.

The lifecycle of an Activity ends with “Activity shut down,” which occurs when the user shuts down our app. You can also notice “App process killed” state at which Android OS has considered our activity disposable, because, for example, the user has left our app and is now doing something else on their device. Smartphones have only a limited amount of resources, i.e. power and memory; therefore, Android must be efficient and decisive at assigning those resources to the appropriate apps. Once Android thinks our user is done using our app, all our activities and the app itself will get killed.

Our example Activity above overrides the onCreate method. Notice that we first use the super  keyword and call the method in the parent class. Then, we proceed to setContent, which, as you can probably imagine, is responsible for setting contents of the screen (layout).

Manifest

Every single Android app must have an AndroidManifest.xml file. It’s an important file that, among many other things, it contains:

  • App’s package name
  • Names of all activities in the app
  • Permissions required to fully operate the app (e.g. camera, GPS)

Fortunately for user privacy, and unfortunately for app developers, from Android 6.0 (API level 23) upwards, it’s not sufficient enough to simply list the required permissions in the Manifest file. To protect users' privacy, we must explicitly ask for certain permissions with a pop-up window, such as the one below.

Image title

App Resources

Resources are the additional files and static content that you may want to use in your app, for example:

  • Images

  • Layout definitions

  • User interface strings

  • Animation instructions

  • And more

Layout definitions are XML files that allow us to easily define screen layouts and are completely separate from our Kotlin code, which is great. It’s always good practice to separate the view from the business logic.

User interface strings are also XML files containing elements that you will refer to in your code, instead of hardcoding them. You want to use those because they promote reusability and make translating your app to different languages a breeze.

Project Structure

Let’s investigate the structure of our project. The Project window in Android Studio gives us a quick overview of all of the files in the project. At the root of the project, we have two folders:

  • app
  • Gradle Scripts

Image title

Gradle Scripts contains everything related to the successful building and packaging of our app. For now, let’s focus on the ‘app’ folder.

Image title

You can see the manifests folder where our manifest file resides. It’s possible to have multiple manifest files but keep everything in one file for as long as you can.

Next up is the Java folder. Don’t let that mislead you. That’s where all of our code will reside whether it’s Java or Kotlin.

The last folder we really care about is res. You can probably guess that’s where we will keep our app’s resources.

Conclusion

In this tutorial, we’ve been introduced to some fundamental concepts of Android development. You now know what Activity is. You are aware of its lifecycle and you know why we need a manifest file. You also learned more about app resources and you understand the basic structure of our project.

In the next tutorial, we will actually get to writing some code and start by creating some layouts for our app. Stay tuned!

Topics:
kotlin ,android studio ,android development ,avd ,activity lifecycle ,activity ,manifest ,Java ,Kotlin tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}