Activity Lifecycle

PHOTO EMBED

Wed Nov 20 2024 13:15:25 GMT+0000 (Coordinated Universal Time)

Saved by @signup_returns

//MainActivity.kt

package com.example.lifecycle

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.lifecycle.R

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) // Set the content view to the layout file
        Toast.makeText(applicationContext, "ONCREATE() CALLED", Toast.LENGTH_SHORT).show()
    }


    override fun onStart() {
        super.onStart()
        Toast.makeText(applicationContext, "ONSTART() CALLED", Toast.LENGTH_SHORT).show()
    }


    override fun onRestart() {
        super.onRestart()
        Toast.makeText(applicationContext, "ONRESTART() CALLED", Toast.LENGTH_SHORT).show()
    }


    override fun onResume() {
        super.onResume()
        Toast.makeText(applicationContext, "ONRESUME() CALLED", Toast.LENGTH_SHORT).show()
    }


    override fun onStop() {
        super.onStop()
        Toast.makeText(applicationContext, "ONSTOP() CALLED", Toast.LENGTH_SHORT).show()
    }

    override fun onDestroy() {
        super.onDestroy()
        Toast.makeText(applicationContext, "ONDESTROY() CALLED", Toast.LENGTH_SHORT).show()
    }
}


//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:gravity="center">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demonstration of ACTIVITY LIFE CYCLE Methods"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_marginBottom="16dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />

</LinearLayout>
content_copyCOPY