Junior — Middle
What is the role of the onCreate method in the Activity lifecycle in Android?.
sobes.tech AI
Answer from AI
The onCreate method in the Android Activity lifecycle is called when the Activity is created. It is the first method where the Activity's components are initialized.
In onCreate, the following are usually performed:
- Setting up the user interface via
setContentView. - Initializing variables and components.
- Configuring event listeners.
- Restoring the Activity's state if it was saved.
Example:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI and logic
}
Thus, onCreate is the entry point for preparing the Activity for display and user interaction.