Skip to content

Activity vs. Fragment

Question

Activity vs fragment

Short interview answer

An activity is an Android entry point for user interaction and participates directly in the system lifecycle. A fragment is reusable UI behavior hosted by an activity or another fragment and has its own lifecycle, including a separate view lifecycle.

Detailed answer

An activity is a system entry point; a fragment is hosted UI that must be managed through the host’s FragmentManager:

SupportFragmentManager.BeginTransaction()
    .Replace(Resource.Id.content, new InvoiceFragment())
    .AddToBackStack(null)
    .Commit();

Do not treat a fragment as a miniature activity: its view can be destroyed while the fragment object remains, so clear view references at the appropriate lifecycle boundary.

Sources