Junior — Middle
Can the main characteristics of the Activity component's lifecycle in Android be listed?
sobes.tech AI
Answer from AI
The activity lifecycle in Android consists of several key states and methods called by the system when the component's state changes:
- onCreate() — called when the activity is created, where UI and data initialization occurs.
- onStart() — activity becomes visible to the user.
- onResume() — activity starts interacting with the user (foreground).
- onPause() — called when the activity loses focus partially, e.g., when a dialog appears; data should be saved here.
- onStop() — activity is no longer visible; resources can be released.
- onDestroy() — final destruction of the activity, resources are freed.
- onRestart() — called when the activity is restarted after being stopped.
Example sequence of calls:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
}
override fun onResume() {
super.onResume()
}
override fun onPause() {
super.onPause()
}
override fun onStop() {
super.onStop()
}
override fun onDestroy() {
super.onDestroy()
}
override fun onRestart() {
super.onRestart()
}
Understanding the lifecycle is crucial for proper resource management and state preservation.