Skip to main content

Introduction to Activity in Android

Introduction to Activity in Kotlin Android

In Android development, an Activity is a core component that represents a single screen with a user interface. It is responsible for handling user interactions, displaying views, and managing the lifecycle of the application.

An activity acts as a mediator between the user and the application, allowing users to interact with the app by receiving input and displaying output on the screen. Each activity is independent and can be started or stopped without affecting other activities.

Features of Activity

  1. UI Management: Activities are responsible for managing the user interface of the application. They can display views, handle user input, and update the UI based on user interactions.

  2. Lifecycle Management: Activities have a well-defined lifecycle that allows developers to handle important events like creation, starting, pausing, resuming, stopping, and destroying of the activity. This lifecycle ensures that the app behaves correctly and efficiently.

  3. Intent Handling: Activities can receive intents from other components, such as other activities or system services. Intents allow activities to start other activities, pass data between them, or perform actions based on the received intent.

  4. Back Stack Management: Activities are organized in a stack called the "back stack." When a new activity is started, it is pushed onto the stack, and when the user navigates back, the top activity is popped from the stack, revealing the previous activity.

Usage of Activity in Kotlin Android

To create an activity in Kotlin Android, follow these steps:

  1. Create a new Kotlin class: Create a new Kotlin class by right-clicking on the package where you want to create the activity. Choose "New" > "Kotlin Class" and give it a name (e.g., MainActivity).

  2. Extend the AppCompatActivity class: In the newly created class, extend the AppCompatActivity class to inherit the features of the Android support library.

class MainActivity : AppCompatActivity() {
// Activity code goes here
}
  1. Override the necessary lifecycle methods: Override the necessary lifecycle methods to handle the activity's lifecycle events. Some commonly used methods include onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy().
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the activity layout here
setContentView(R.layout.activity_main)

// Initialize views and perform any other setup tasks
}

override fun onStart() {
super.onStart()
// Perform tasks when the activity is about to become visible
}

override fun onResume() {
super.onResume()
// Perform tasks when the activity is in the foreground
}

override fun onPause() {
super.onPause()
// Perform tasks when the activity is about to be paused
}

override fun onStop() {
super.onStop()
// Perform tasks when the activity is no longer visible
}

override fun onDestroy() {
super.onDestroy()
// Perform tasks when the activity is being destroyed
}
}
  1. Set the activity layout: In the onCreate() method, set the activity layout using the setContentView() method. This method takes a layout resource ID as a parameter and inflates the corresponding XML layout file.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize views and perform any other setup tasks
}
  1. Handle user interactions: Inside the activity, you can handle user interactions by adding event listeners to views and performing actions based on user input. For example, you can add a click listener to a button and perform some action when the button is clicked.
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
// Perform action when the button is clicked
}
  1. Start another activity: To start another activity from the current activity, create an Intent and call the startActivity() method, passing the intent as a parameter. You can also pass data to the new activity using the intent's extras.
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", value)
startActivity(intent)

These are the basic steps to create and use an activity in Kotlin Android. By understanding the activity lifecycle and utilizing its features, you can build interactive and responsive applications.

Remember to declare your activities in the AndroidManifest.xml file to make them accessible to the Android system.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">

<application>
<activity android:name=".MainActivity">
<!-- Additional activity configuration goes here -->
</activity>
</application>
</manifest>

That's it! You now have a solid understanding of activities in Kotlin Android and how to use them to build dynamic and engaging user interfaces.

More Android Activity Examples