Junior — Middle
What is the function of Activity components in Android and in what context are they used?
sobes.tech AI
Answer from AI
An Activity in Android is one of the main components of an application, representing a screen with a user interface. It serves as the point of user interaction with the app.
Functions of an Activity:
- Displaying the UI and handling user interactions.
- Managing the screen's lifecycle (creation, pause, resume, destruction).
- Launching other Activities and receiving results from them.
Context of use:
Each screen or individual user thread is usually implemented as a separate Activity. For example, a login screen, a product list, product details — can be different Activities.
Example of a simple Activity in Kotlin:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Thus, an Activity is a basic building block of the interface in Android, providing user interaction with the application.