EDDYMENS

Published a month ago

What Is An Activity In Android Development?

Table of contents

Definition

An Activity in Android is a single screen with a user interface. Think of it as the main point where users interact with the app. For example, an email app might have one Activity showing a list of emails, another Activity to compose a new email, and another Activity for reading emails. Each screen is a separate Activity, but they work together to create a smooth experience for the user, kind of similar to individual web pages.

The Lifecycle of an Activity

Understanding the lifecycle of an Activity is important for making good Android apps. An Activity goes through several stages during its life, managed by a series of lifecycle methods. Here are the key stages:

  • onCreate(): Called when the Activity is first created. This is where you set up the user interface and allocate resources.
  • onStart(): Called just before the Activity becomes visible to the user.
  • onResume(): Called just before the user starts interacting with the Activity. At this point, the Activity is on top and ready for user input.
  • onPause(): Called when the system is about to put the Activity into the background, usually because another Activity is starting.
  • onStop(): Called when the Activity is no longer visible to the user.
  • onDestroy(): Called before the Activity is destroyed. This is where you clean up resources.
  • onRestart(): Called after the Activity has been stopped and is about to start again.

Activity States

Activities can be in different states, and understanding these states helps in managing resources and ensuring a smooth user experience. The key states include:

  • Active/Running: The Activity is in the foreground, interacting with the user.
  • Paused: The Activity is partially hidden but still visible. It does not get user input.
  • Stopped: The Activity is completely hidden and not visible, but its state is kept.
  • Destroyed: The Activity is ended, and the system reclaims its resources.

Creating and Managing Activities

Creating an Activity involves making a subclass of the Activity class and overriding its lifecycle methods to perform necessary tasks. Here is a simple example of an Activity:

01: public class MainActivity extends AppCompatActivity { 02: @Override 03: protected void onCreate(Bundle savedInstanceState) { 04: super.onCreate(savedInstanceState); 05: setContentView(R.layout.activity_main); 06: // Initialize UI components and resources here 07: } 08: 09: @Override 10: protected void onStart() { 11: super.onStart(); 12: // Code to execute when the activity is starting 13: } 14: 15: @Override 16: protected void onResume() { 17: super.onResume(); 18: // Code to execute when the activity is resuming 19: } 20: 21: @Override 22: protected void onPause() { 23: super.onPause(); 24: // Code to execute when the activity is pausing 25: } 26: 27: @Override 28: protected void onStop() { 29: super.onStop(); 30: // Code to execute when the activity is stopping 31: } 32: 33: @Override 34: protected void onDestroy() { 35: super.onDestroy(); 36: // Code to execute when the activity is being destroyed 37: } 38: }

In Android, moving between Activities is done using Intents. An Intent is a description of an operation to be performed. Here’s how you can start a new Activity:

01: Intent intent = new Intent(this, SecondActivity.class); 02: startActivity(intent);

You can also pass data between Activities using Intents. For example:

01: Intent intent = new Intent(this, SecondActivity.class); 02: intent.putExtra("EXTRA_MESSAGE", "Hello, Second Activity!"); 03: startActivity(intent);

In the receiving Activity, you can get the data:

01: String message = getIntent().getStringExtra("EXTRA_MESSAGE");

Conclusion

Activities are the backbone of Android apps, providing a structure for user interactions and navigation. By understanding the lifecycle and states of an Activity, developers can create efficient and responsive apps that offer a smooth user experience. Whether you’re managing resources, handling user inputs, or moving between different screens, mastering Activities is crucial for any Android developer.

Here is another article you might like 😊 What Are CSS Container Queries?