0
0
Android Kotlinmobile~20 mins

Why understanding lifecycle prevents bugs in Android Kotlin - Build It to Prove It

Choose your learning style9 modes available
Build: Lifecycle Demo Screen
This screen shows how Android lifecycle events work and helps prevent bugs by logging lifecycle changes.
Target UI
-------------------------
| Lifecycle Demo Screen  |
|-----------------------|
| Logs:                 |
|                       |
| [Lifecycle events...] |
|                       |
|-----------------------|
| [Clear Logs Button]    |
-------------------------
Display a TextView that shows lifecycle event logs
Log lifecycle events: onCreate, onStart, onResume, onPause, onStop, onDestroy
Add a button to clear the logs
Use proper lifecycle methods to update the log
Prevent bugs by showing how lifecycle affects UI updates
Starter Code
Android Kotlin
package com.example.lifecycledemo

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

class LifecycleDemoActivity : AppCompatActivity() {
    private lateinit var logTextView: TextView
    private lateinit var clearButton: Button

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

        logTextView = findViewById(R.id.logTextView)
        clearButton = findViewById(R.id.clearButton)

        clearButton.setOnClickListener {
            // TODO: Clear the logs
        }

        // TODO: Log onCreate event
    }

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

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

class LifecycleDemoActivity : AppCompatActivity() {
    private lateinit var logTextView: TextView
    private lateinit var clearButton: Button

    private fun logEvent(event: String) {
        logTextView.append("$event\n")
    }

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

        logTextView = findViewById(R.id.logTextView)
        clearButton = findViewById(R.id.clearButton)

        clearButton.setOnClickListener {
            logTextView.text = ""
        }

        logEvent("onCreate")
    }

    override fun onStart() {
        super.onStart()
        logEvent("onStart")
    }

    override fun onResume() {
        super.onResume()
        logEvent("onResume")
    }

    override fun onPause() {
        super.onPause()
        logEvent("onPause")
    }

    override fun onStop() {
        super.onStop()
        logEvent("onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        logEvent("onDestroy")
    }
}

This app logs each lifecycle event to a TextView. By seeing these logs, you understand when each lifecycle method runs.

This helps prevent bugs because you know when to update UI or release resources. For example, updating UI in onResume ensures the screen is visible. Clearing logs with a button shows how user actions can interact with lifecycle.

Using lifecycle methods properly avoids crashes or unexpected behavior when the app moves between states.

Final Result
Completed Screen
-------------------------
| Lifecycle Demo Screen  |
|-----------------------|
| Logs:                 |
| onCreate              |
| onStart               |
| onResume              |
|                       |
|-----------------------|
| [Clear Logs Button]    |
-------------------------
When the screen opens, lifecycle events onCreate, onStart, onResume are logged.
When the app goes to background, onPause and onStop are logged.
When the app is closed, onDestroy is logged.
Tapping Clear Logs button clears all logged events from the screen.
Stretch Goal
Add a Toast message showing the current lifecycle event each time it happens.
💡 Hint
Use Toast.makeText(this, "EventName", Toast.LENGTH_SHORT).show() inside each lifecycle method.