MΓ³dulo 00 Β· Welcome & Setup β LecciΓ³n 2 de 5 Β· ~5 min
Your first look at Kotlin
Reading MainActivity.kt, piece by piece
Let's decode that file you just saw, one keyword at a time β no prior Kotlin needed.
class MainActivity : ComponentActivity()β declares aclassnamedMainActivitythat inherits fromComponentActivity, Android's base class for a screen with its own lifecycle. The colon means "is a kind of."override fun onCreate(savedInstanceState: Bundle?)βfundeclares a function (Kotlin's word for what other languages call a method).overridemeans this replaces a functionComponentActivityalready defines β Android callsonCreateonce, when the screen is first created. The?afterBundlemeans the parameter is nullable: it might be aBundle, or it might benull. Much more on that in Module 2.setContent { β¦ }β the bridge into Jetpack Compose. Everything inside those curly braces describes the UI declaratively β you say what the screen looks like for the current state, not a sequence of steps to build it.PawWalkTheme { β¦ },Surface(...)β these are@Composablefunctions: ordinary Kotlin functions marked as UI-describing. Compose calls them to draw the screen, and re-calls them ("recomposes") whenever the state they read changes.
Compose vs SwiftUI, side by side
If you've done (or plan to do) the iOS track, the ideas map almost one-to-one:
| Concept | SwiftUI | Jetpack Compose |
|---|---|---|
| UI building block | struct conforming to View | @Composable function |
| Declare a constant | let | val |
| Declare a variable | var | var |
| Local UI state | @State | remember { mutableStateOf(...) } |
| Re-render on state change | automatic re-render | recomposition |
| Vertical stack | VStack | Column |
| Horizontal stack | HStack | Row |
| String interpolation | "\(name)" | "$name" |
Same job, different spelling. You'll see this table's rows again as real code throughout the course.