[Kotlin] Anatomy and Life Cycle of Android Applications

Date:     Updated:

Categories:

Tags:

📋 Here are the notes summarizing what I learned from the course!


Anatomy and Life Cycle of Android Applications

Objectives:

  • Explain Android activities, fragments, intents.
  • Understand application, activity, and fragment life cycles.
  • Create and use activities.
  • Apply intents to call built-in applications and pass information to other activities.
  • Create and use fragments.

Activities

Definition

  • Activity: An application component that provides a screen for user interaction to do things like dialing the phone, taking a photo, sending an email, or viewing a map. Each activity is given a window to draw its user interface.

Creating an Activity

  • To create an activity, create a subclass of Activity or AppCompatActivity for compatibility support.
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Registering an Activity in the Manifest

  • Activities need to be registered in the Android manifest with necessary metadata like labels and themes.
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

Using AppCompatActivity

  • AppCompatActivity provides backward compatibility for features added in newer releases of the Android platform.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}

Activity States and Lifecycle Callbacks

Lifecycle States

  • Nonexistent
  • Created
  • Started
  • Resumed
  • Destroyed

Example: Lifecycle Callbacks

  • Lifecycle methods like onCreate, onStart, onResume, onPause, onStop, and onDestroy handle transitions between states.
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d(TAG, "onCreate(Bundle?) called")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart() called")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume() called")
    }
    // Implement onPause, onStop, onDestroy as needed
}

Managing Activity Transitions with Intents

Intents

  • Intents are asynchronous messages used to request an action from another app component.

Example: Starting an Activity

val intent = Intent(this, ActivityB::class.java)
startActivity(intent)

Passing Data with Intents

val intent = Intent(this, DisplayMessageActivity::class.java)
intent.putExtra("EXTRA_MESSAGE", "Hello World!")
startActivity(intent)

Fragments

Definition

  • Fragment: A modular section of an activity, which has its own lifecycle and can be added or removed while the activity is running.

Example: Creating a Fragment

class AboutFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_about, container, false)
    }
}

Fragment Lifecycle

States

  • Fragments transition through various states in their lifecycle, similar to activities.

Adding and Managing Fragments

Example: Adding a Fragment to an Activity

<FragmentContainerView
    android:id="@+id/fragment_container_view"
    android:name="com.example.android.fragments.AboutFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

References




Back to Top

See other articles in Category Kotlin

Leave a comment