DevBits
DevBitsAndroid Handbook
Android Fundamentals

What is the difference between an Activity and a Fragment?

Explain the different responsibilities of Activities and Fragments in modern Android.

AF-003Foundational~30 sec

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

ActivityFragment
Owns the application windowOwns a portion of the UI
Started by the Android system via an IntentHosted by an Activity
Participates in the task and back stackDepends on the host Activity for the window
Can host Fragments or Compose UIHas its own Fragment lifecycle and a separate view lifecycle
Represents the application entry pointRepresents 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?

ScenarioRecommended component
Application entry pointActivity
Host application navigationActivity
Reusable screen sectionFragment
Single-Activity applicationActivity + 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.

Follow-up questions

What separates a senior answer

Key takeaways

Learn more

On this page