DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Introduction to Android App Development With Kotlin: Activity and its Lifecycle (Part 2)

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.

Łukasz Mądrzak user avatar by
Łukasz Mądrzak
CORE ·
Feb. 14, 19 · Tutorial
Like (8)
Save
Tweet
Share
12.60K Views

Join the DZone community and get the full member experience.

Join For Free

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!

app Android (robot) Kotlin (programming language) Manifest file

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integration: Data, Security, Challenges, and Best Solutions
  • Explaining: MVP vs. PoC vs. Prototype
  • Internal Components of Apache ZooKeeper and Their Importance
  • DevOps Roadmap for 2022

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: