An Activity is one screen of your app. Android constantly creates, pauses and destroys activities as the user navigates, rotates the phone, or takes a call — so you must respond to lifecycle events.
The lifecycle callbacks
onCreate()— set up UI, run once when the screen is created.onStart()/onResume()— screen becomes visible / interactive.onPause()/onStop()— screen is leaving; save state, release resources.onDestroy()— screen is finished or being recreated.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onPause() {
super.onPause()
// save unsaved data here
}
}
Why it matters: configuration changes
Rotating the screen destroys and recreates the Activity by default — losing in-memory data. The modern fix is to keep UI state in a ViewModel (covered later), which survives rotation.
Common mistake: Never do long work (network, database) directly in onCreate on the main thread — it freezes the UI. Use coroutines.
Summary
Activities are screens with a predictable lifecycle. Respond to the right callbacks and keep important state outside the Activity.