Activity vs Fragment in Android: Key Differences and Usage
Activity is a single focused screen that users interact with, while a Fragment is a reusable portion of UI inside an Activity. Activities manage the app's overall window, and fragments allow modular UI design within those windows.Quick Comparison
This table summarizes the main differences between Activity and Fragment in Android.
| Factor | Activity | Fragment |
|---|---|---|
| Definition | A full screen or window representing a single UI | A reusable UI component inside an Activity |
| Lifecycle | Has its own independent lifecycle | Lifecycle depends on the hosting Activity |
| UI Handling | Manages entire screen layout | Manages part of the screen layout |
| Navigation | Can be started independently | Cannot exist without an Activity |
| Use Case | Represents a standalone screen | Used for modular and flexible UI |
| Communication | Communicates via Intents | Communicates via Activity or ViewModel |
Key Differences
Activity is the main building block of an Android app's user interface. It represents a single screen with which the user can interact. Each Activity has its own lifecycle methods like onCreate(), onStart(), and onDestroy() that manage its state independently.
Fragment is a smaller piece of an Activity. It cannot run on its own and must be hosted inside an Activity. Fragments have their own lifecycle but it is closely tied to the Activity lifecycle. They allow developers to create flexible and reusable UI components that can be combined or replaced dynamically within an Activity.
While Activities handle navigation and app-wide events, Fragments are ideal for managing parts of the UI, such as tabs or sections, enabling better UI modularity and reuse. Communication between Activities is done via Intents, whereas Fragments communicate through their hosting Activity or shared ViewModels.
Code Comparison
Here is a simple example showing how an Activity displays a text on screen.
import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val textView = TextView(this) textView.text = "Hello from Activity" setContentView(textView) } }
Fragment Equivalent
Here is how a Fragment displays the same text inside an Activity.
import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment class HelloFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val textView = TextView(requireContext()) textView.text = "Hello from Fragment" return textView } }
When to Use Which
Choose Activity when you need a full screen or independent UI that manages its own lifecycle and navigation. Activities are best for distinct app screens like login, settings, or main content.
Choose Fragment when you want to build reusable, modular UI components inside an Activity. Fragments are ideal for dynamic layouts, multi-pane views, or when you want to swap parts of the UI without changing the whole screen.
In modern Android apps, use Activities as containers and Fragments for flexible UI pieces to improve code reuse and user experience.