Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the difference between Activity, Fragment and Service?

  • Activity — a single screen with a user interface, and an entry point into the app. It has its own lifecycle and appears in the back stack. Modern apps typically use few Activities — often a single one hosting many destinations.
  • Fragment — a reusable portion of UI hosted inside an Activity, with its own lifecycle tied to the host's. Fragments exist so that one Activity can present different content — essential for tablets and foldables where two panes appear side by side, and the basis of Navigation Component destinations. The critical subtlety is that a Fragment has two lifecycles: the Fragment itself, and its view, which is destroyed and recreated when the Fragment goes onto the back stack. Observing LiveData with the Fragment's lifecycle rather than viewLifecycleOwner is a classic bug that causes duplicate observers.
  • Service — a component for work with no user interface, running in the background. Foreground services show a persistent notification and are for user-visible ongoing work such as music playback or navigation. Bound services provide an interface for other components to call.

Note: The important modern point about Services: background execution limits since Android 8 mean a plain background Service is no longer a reliable way to do deferred work. WorkManager is the correct API for guaranteed background work, and a foreground service is required for anything long-running and user-visible. Saying you would use a Service for periodic sync is a dated answer.

All Android interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as