What is the difference between an Activity and a Fragment?
Explain the different responsibilities of Activities and Fragments in modern Android.
What the interviewer is testing
The interviewer is checking whether you understand ownership and responsibility rather than simply comparing APIs or lifecycle callbacks.
A good answer distinguishes an Activity's responsibility for the window and system entry point from a Fragment's responsibility for a portion of UI inside that Activity. It should also reflect modern Android, where many applications use one Activity and compose their screens with Fragments or Jetpack Compose.
❌ Common mistake
If they ask you to elaborate
| Activity | Fragment |
|---|---|
| Owns the application window | Owns a portion of the UI |
| Started by the Android system via an Intent | Hosted by an Activity |
| Participates in the task and back stack | Depends on the host Activity for the window |
| Can host Fragments or Compose UI | Has its own Fragment lifecycle and a separate view lifecycle |
| Represents the application entry point | Represents reusable UI and behaviour |
A common modern architecture is a single Activity that hosts navigation. Compose-first applications often use a single Activity and may not use Fragments at all, while many production applications still rely on Fragments for modular navigation and reusable UI.
When should you use each?
| Scenario | Recommended component |
|---|---|
| Application entry point | Activity |
| Host application navigation | Activity |
| Reusable screen section | Fragment |
| Single-Activity application | Activity + Fragments (or Compose destinations) |
Code example
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
// Activity owns the window and container.
replace(R.id.fragmentContainer, HomeFragment())
}
}
}
}
class HomeFragment : Fragment(R.layout.fragment_home)The Activity owns the window and the container. HomeFragment contributes one portion of the UI inside that container. The savedInstanceState == null check prevents adding the same Fragment again after configuration changes because the FragmentManager automatically restores existing Fragments.