What is an Activity in Android?

An Activity in Android is one of the key components of an Android application. It represents a single screen in an app with which the user can interact. In simpler terms, an Activity is like a window or a page in a website, where each Activity typically provides some UI elements such as buttons, text fields, images, etc., that the user interacts with.

Key Points About Activities:

  1. UI Representation: An Activity displays the user interface (UI) for a specific interaction. For example, a login screen, a home screen, or a settings page is all typically represented by different Activities.
  2. Life Cycle: Every Activity in Android has a lifecycle, meaning it goes through different stages during its existence. The system creates, pauses, resumes, and destroys an Activity depending on the user interaction or system resource needs. Key lifecycle methods include:
    • onCreate(): Called when the Activity is first created.
    • onStart(): Called when the Activity becomes visible.
    • onResume(): Called when the Activity starts interacting with the user.
    • onPause(): Called when the Activity is partially obscured, but still visible.
    • onStop(): Called when the Activity is no longer visible.
    • onDestroy(): Called when the Activity is being destroyed.
  3. Interaction: Activities are responsible for user interaction within the app. For example, if the user clicks a button on the screen, an Activity might perform an action, like navigating to a new screen or displaying a message.
  4. Starting Another Activity: You can start a new Activity from another Activity using Intents. For example, clicking a button in one Activity might take the user to a second Activity. Intent intent = new Intent(CurrentActivity.this, NewActivity.class); startActivity(intent);
  5. Back Stack: When multiple Activities are started, they are placed on a back stack. The user can navigate back to the previous Activity by pressing the device’s back button, which pops the top Activity off the stack and shows the one beneath it.

Activity Example:

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // This is where you can set up UI elements and event listeners
        Button myButton = findViewById(R.id.myButton);
        myButton.setOnClickListener(v -> {
            // Start a new Activity when the button is clicked
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        });
    }
}

In this example, MainActivity is an Activity. When the button is clicked, a new Activity (SecondActivity) is started using an Intent.

When Do You Need Multiple Activities?

  • Complex apps with different sections might require multiple Activities. For example, a shopping app might have different Activities for browsing products, viewing the cart, and checking out.
  • Navigation: You can use Activities to handle navigation between different parts of your app.

Conclusion

In summary, an Activity is the building block of an Android app’s user interface. It manages user interaction and the screen content for that interaction. Understanding Activities and their lifecycle is fundamental to building Android apps.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top