0
0
Android Kotlinmobile~20 mins

Activity lifecycle methods (onCreate, onStart, onResume, onPause, onStop, onDestroy) in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Lifecycle Demo Screen
This screen shows messages on the screen each time an activity lifecycle method is called. It helps you see when onCreate, onStart, onResume, onPause, onStop, and onDestroy happen.
Target UI
-------------------------
| Lifecycle Demo Screen  |
|-----------------------|
| Messages:             |
| - onCreate called     |
| - onStart called      |
| - onResume called     |
|                       |
| (Messages update live) |
-------------------------
Display a TextView that updates with the name of the lifecycle method called
Override all six lifecycle methods: onCreate, onStart, onResume, onPause, onStop, onDestroy
Each method appends its name to the TextView on a new line
Use setContentView with a simple layout containing only the TextView
Ensure the TextView scrolls if messages get too long
Starter Code
Android Kotlin
package com.example.lifecycledemo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var lifecycleTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // TODO: Set content view and initialize lifecycleTextView
    }

    // TODO: Override other lifecycle methods here
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.lifecycledemo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var lifecycleTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        lifecycleTextView = findViewById(R.id.lifecycleTextView)
        appendMessage("onCreate called")
    }

    override fun onStart() {
        super.onStart()
        appendMessage("onStart called")
    }

    override fun onResume() {
        super.onResume()
        appendMessage("onResume called")
    }

    override fun onPause() {
        super.onPause()
        appendMessage("onPause called")
    }

    override fun onStop() {
        super.onStop()
        appendMessage("onStop called")
    }

    override fun onDestroy() {
        super.onDestroy()
        appendMessage("onDestroy called")
    }

    private fun appendMessage(message: String) {
        lifecycleTextView.append("$message\n")
    }
}

We override all six lifecycle methods to track when each is called. In each method, we add a line of text to the TextView to show the event. The TextView is initialized in onCreate after setting the content view. This way, as the activity moves through its lifecycle, the user sees messages updating live on the screen.

The layout file activity_main.xml should contain a ScrollView with a TextView inside to allow scrolling when messages overflow.

Final Result
Completed Screen
-------------------------
| Lifecycle Demo Screen  |
|-----------------------|
| Messages:             |
| onCreate called       |
| onStart called        |
| onResume called       |
|                       |
| (Scroll if needed)    |
-------------------------
When the app starts, 'onCreate called', 'onStart called', and 'onResume called' messages appear in order.
When the app goes to background, 'onPause called' and 'onStop called' appear.
When the app is closed, 'onDestroy called' appears.
Messages keep appending so user can see lifecycle flow.
Stretch Goal
Add a button that clears all lifecycle messages from the TextView.
💡 Hint
Add a Button below the TextView in the layout. Set an onClickListener that sets lifecycleTextView.text = "".