Android  Activity and its Lifecycle

Welcome, here we are going to learn about the first primary component of Android which is, Activity. There is a total of 4 Android Components, and we will be discussing them one by one.

When we all started with coding, we know about the main method from where the program begins execution. Similarly, in Android, Activity is the one from where the Android Application starts its process. Activity is one screen of the app's user interface. There is a series of methods which runs in an activity.

We will be learning about the lifecycle of an Activity:

The cycle consists of 7 methods:

  1. onCreate() — called when an activity first created; when a user opens the app
  2. onStart() — called when an activity becomes visible to the user
  3. onResume() — called when a user starts interacting with the application
  4. onPause() — when activity is paused and receiving other activities in onResume method
  5. onStop() — called when activity is no longer visible to the user; when a user minimizes the app
  6. onDestroy() — called when activity destroyed, when the user clears the application stack
  7. onRestart() — called when activity restarts

So, here we can understand the single screen as a single activity. In previous chapters, you had created screens; here we are going to connect them with our kotlin code and make them intractable.

Before starting with the development, we also need to understand about Intents.

Intents — these are the messaging body using which we can communicate between all the Android components. Intent also carries the information to pass between the components. Intents are of two types:

  1. Implicit — opening another capable app which can perform the required actions from one application, like Google Maps to show map or YouTube to play video etc.
  2. Explicit — opening another activity within the application, like opening settings screen in your app.

Let’s start with developing our application in kotlin. We are going to develop an app which consists of three screens and on opening the app, we will see the first screen with two buttons clicking on which will open those other two screens.

  1. Open Android Studio > Create New Project. If you already had a project open then goto the file option in top left menu and select New > New Project.
  2. Following the same points done in the previous chapters to create project like giving name to app, setting up the sdk etc.
  3. By default you will get a MainActiivty.kt file in Java folder and activity_main.xml in res > layout folder. These both file together makes one activity.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnMindorks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MindOrks"
    app:layout_constraintBottom_toTopOf="@+id/btnGetmeanapp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnGetmeanapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="GetMeAnApp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnMindorks" />

</android.support.constraint.ConstraintLayout>

4. Here, we have two buttons who says - "MindOrks" and "GetMeAnApp". This layout is in Constraint Layout.

5. Open you MainActivity.kt which is your kotlin file and we will connect it with the activity_main.xml file. To connect the kotlin and the layout file we need to put the following line of code in onCreate method. This is the in-built method by Android.

setContentView(R.layout.activity_main)

5. Now, we need to create the other two activities. Creating one kotlin file in java folder as sibling to MainActivity and activit_test.xml in res folder. To create kotlin class, you can simply right click on subpackge inside Java and New > kotlin file.

Yoou can only use lowercase a-z, 0-9 and underscore_ to name the layout file.

6. Inside the two newly created xml layout files you can put one TextView so that when you open them you can see different texts.

activiy_getmeanapp.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GetMeAnApp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

activity_mindorks.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MindOrks"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

7. Now connect these two layouts with their respective classes

// GetmeanappActivity.kt
class GetmeanappActivity : AppCompatActivity() {

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

// MindorksActivity.kt
class MindorksActivity : AppCompatActivity() {

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

8. Now, Important part. How to open these two new screens. We had created two buttons in MainActivity.kt, so now we will set the listener on those buttons and whenever the user click on it, screen gets open.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
// opening MindorksActivity
        btnMindorks.setOnClickListener {
            var mindorksIntent = Intent(this, MindorksActivity::class.java)
            startActivity(mindorksIntent)
        }
// opening GetmeanappActivity
        btnGetmeanapp.setOnClickListener {
            var getmeanappIntent = Intent(this, GetmeanappActivity::class.java)
            startActivity(getmeanappIntent)
        }
    }
}

9. Here, btnMindorks and btnGetmeanapp are the id of the buttons which we had already setup in activity_main.xml where we created these buttons.

We had created a object from Intent class and put two arguments in it.

A. this - from which context it will open the new activity, so specify this as a reference to the MainActivity

B. MindorksActivity::class.java / Getmeanapp::class.java - the activity that we need to open

startActivity() - passing the intent object in this method will launch the new acitivty.

10 . Last, you need to mention all the activity which are in the app inside the manifest file.

You can open Manifest.xml file and MainActivity is already define in it when we created the project. Now, I need to add my two new acitivty in it.

<activity android:name=".GetmeanappActivity" />
<activity android:name=".MindorksActivity" />

I will add them at the bottom after the last activity tag ends.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".GetmeanappActivity" />
    <activity android:name=".MindorksActivity" />

</application>

Congratulations!! You had created your first Android Application with user interaction. You learnt the concept of Activity and its lifecycle.

Now you can run this application and try to click the buttons. Also try to do some more experiment with it and share with us.

In next chapter you are going to learn about how to show websites in your activities.

Happy Learning :)

Team Mindorks.