Android1/3/2020

Understanding Activities, Fragments, and Intents in Android

Activities, Fragments, and Intents are some of the most important building blocks of Android application development.

Every Android developer must understand how these components work together to build scalable and responsive mobile applications.

In this guide, we will learn:

  • What Activities are
  • Activity lifecycle methods
  • What Fragments are
  • How Intents work
  • Modern Android development best practices

What Is an Activity in Android?

An Activity represents a single user interface screen in an Android application.

Examples:

  • Login screen
  • Home screen
  • Profile screen
  • Settings page

Activities are responsible for:

  • Displaying UI
  • Handling user interaction
  • Managing lifecycle events
  • Launching other screens

Why Activities Are Important

Activities act as the entry point for user interaction in Android applications.

Every Android app usually contains multiple activities connected together through navigation flows.


Declaring Activities in AndroidManifest.xml

Every Activity must be declared inside:


AndroidManifest.xml

Example:


<activity
    android:name=".MainActivity" />

Android Activity Lifecycle

Android manages Activities using lifecycle methods.

These lifecycle callbacks help developers:

  • Manage memory
  • Handle app state
  • Pause background tasks
  • Restore UI state

Main Activity Lifecycle Methods

Method Purpose
onCreate() Initialize Activity
onStart() Activity becomes visible
onResume() Activity enters foreground
onPause() Activity partially hidden
onStop() Activity no longer visible
onRestart() Restarting stopped Activity
onDestroy() Cleanup before destruction

Typical Activity Launch Flow

When the app starts:


onCreate()
onStart()
onResume()

Back Button Lifecycle Flow

When user presses Back:


onPause()
onStop()
onDestroy()

Example Activity Lifecycle Logging


public class MainActivity
        extends AppCompatActivity {

    @Override
    protected void onCreate(
            Bundle savedInstanceState
    ) {

        super.onCreate(savedInstanceState);

        Log.d(
                "Lifecycle",
                "onCreate invoked"
        );
    }

    @Override
    protected void onStart() {

        super.onStart();

        Log.d(
                "Lifecycle",
                "onStart invoked"
        );
    }

    @Override
    protected void onResume() {

        super.onResume();

        Log.d(
                "Lifecycle",
                "onResume invoked"
        );
    }

    @Override
    protected void onPause() {

        super.onPause();

        Log.d(
                "Lifecycle",
                "onPause invoked"
        );
    }

    @Override
    protected void onStop() {

        super.onStop();

        Log.d(
                "Lifecycle",
                "onStop invoked"
        );
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        Log.d(
                "Lifecycle",
                "onDestroy invoked"
        );
    }
}

Important Lifecycle Concepts

Foreground State

Activity is interactive during:


onResume()

Visible State

Activity is visible between:


onStart() → onStop()

Entire Lifetime

Activity exists between:


onCreate() → onDestroy()

What Is a Fragment?

A Fragment is a reusable UI component hosted inside an Activity.

Fragments help developers:

  • Create modular UI
  • Support tablets and foldables
  • Reuse components
  • Improve navigation flexibility

Why Modern Apps Use Fragments

Modern Android devices have multiple screen sizes and orientations.

Fragments allow applications to adapt dynamically across:

  • Phones
  • Tablets
  • Foldables
  • Large screens

Fragment Lifecycle

Fragments have their own lifecycle separate from Activities.

Important Fragment methods:

  • onAttach()
  • onCreate()
  • onCreateView()
  • onViewCreated()
  • onStart()
  • onResume()
  • onPause()
  • onDestroyView()
  • onDestroy()

What Is an Intent in Android?

Intent is a messaging object used for communication between Android components.

Intents are used to:

  • Open Activities
  • Start Services
  • Send Broadcasts
  • Launch external applications

Types of Intents


1. Explicit Intent

Explicit Intent directly specifies the target Activity or component.

Example:


Intent intent =
    new Intent(
        MainActivity.this,
        SecondActivity.class
    );

startActivity(intent);

2. Implicit Intent

Implicit Intent specifies an action instead of a class name.

Android automatically finds applications capable of handling the action.

Examples:

  • Opening camera
  • Sending email
  • Opening maps
  • Sharing content

Example — Open Browser Using Implicit Intent


Intent intent =
    new Intent(
        Intent.ACTION_VIEW,
        Uri.parse("https://developer.android.com")
    );

startActivity(intent);

Modern Android Architecture Recommendations

Modern Android development now prefers:

  • Single Activity Architecture
  • Navigation Component
  • Jetpack Compose
  • MVVM Architecture
  • ViewModel
  • StateFlow
  • Coroutines

Common Mistakes Beginners Make

1. Forgetting Activity Declaration

Undeclared Activities cause runtime crashes.


2. Heavy Work in onCreate()

Avoid heavy database or network operations inside:


onCreate()

3. Misusing Fragments

Fragments should remain modular and reusable.


4. Memory Leaks Through Context References

Improper lifecycle handling may cause memory leaks.


Activity vs Fragment

Activity Fragment
Independent screen Reusable UI component
Declared in Manifest Attached to Activity
Own window Shares Activity window
Lifecycle managed by Android Lifecycle tied to Activity

FAQ

Can an app have multiple Activities?

Yes. Most Android applications contain multiple Activities.

Can Fragments exist without Activities?

No. Fragments must be hosted inside Activities.

What is the modern navigation approach?

Navigation Component with Single Activity Architecture is the modern recommended approach.


Conclusion

Activities, Fragments, and Intents form the foundation of Android application architecture.

Understanding lifecycle management and component communication is essential for building scalable and responsive Android applications.

Modern Android development combines these components with lifecycle-aware architecture, Kotlin, Jetpack libraries, and reactive state management for production-grade applications.


About the Author

Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.

CodeChain Dev — Build Modern Products. Solve Real Problems.

Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.