0
0
Android-kotlinComparisonBeginner · 4 min read

Activity vs Fragment in Android: Key Differences and Usage

In Android, an 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.

FactorActivityFragment
DefinitionA full screen or window representing a single UIA reusable UI component inside an Activity
LifecycleHas its own independent lifecycleLifecycle depends on the hosting Activity
UI HandlingManages entire screen layoutManages part of the screen layout
NavigationCan be started independentlyCannot exist without an Activity
Use CaseRepresents a standalone screenUsed for modular and flexible UI
CommunicationCommunicates via IntentsCommunicates 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.

kotlin
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)
  }
}
Output
A screen showing the text: Hello from Activity
↔️

Fragment Equivalent

Here is how a Fragment displays the same text inside an Activity.

kotlin
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
  }
}
Output
A part of the screen showing the text: Hello from Fragment
🎯

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.

Key Takeaways

Activity represents a full screen UI with its own lifecycle and navigation.
Fragment is a reusable UI component hosted inside an Activity with a dependent lifecycle.
Use Activities for standalone screens and Fragments for modular UI parts.
Fragments enable flexible and dynamic UI changes within an Activity.
Communication differs: Activities use Intents, Fragments communicate via Activity or shared ViewModels.