Activity and Fragment - xialin/GrabMovie GitHub Wiki
Overview
Because of Android API's history, you can loosely consider Fragments as UI pieces of a screen. In other words, Fragments are normally related to UI. Activities can be loosely considered to be controllers, they are specially important for their lifecycle and for managing state.
Tips:
- Avoid putting to much code in activities. Instead, keep them as a lightweight container, existing in your application primarily for the lifecycle and other important Android-interfacing APIs.
- Avoid having an activity without a corresponding fragment. Prefer single-fragment activities to plain activities. Put most of the UI code into fragment.
- Avoid using nested fragments.
- Avoid abuse Android-level APIs, e.g. heavily relying on Intent for app's internal workings. (It is known that if your app uses Intents for internal communication between your packages, you might incur multi-second lag on user experience if the app was opened just after OS boot.)
Activity and Lifecycle
Fragment and Lifecycle
Pros vs. Cons
Fragments are reusable components of our application that can be presented to the user in various ways. It is extremely useful when supporting phone + tablet designs, ViewPager and many other features.