This document discusses how to use fragments in Android applications to create dynamic user interfaces that can adapt to different screen sizes and densities. It introduces fragments as reusable modules that represent a portion of UI and can be shared across activities. The key points covered include: what problems fragments solve, how to create fragment subclasses, the fragment lifecycle, how to add fragments to activities dynamically or statically via XML, and how to use the support library for backwards compatibility.
3. 1. Whats the Problem?
2. What Are Fragments?
3. How Do I Use Fragments
4. Look at Code!
5. What about Backwards
Compatibility?
Things Well Talk About
11. User Interface modules
Can share Fragments across different
activity
On Activity can have many
fragments
Different screen sizes have different
layouts
What are Fragments?
13. Android 3.0 or Higher (well, sort of/not really)
Subclass the Fragment base class
They can load Layout files / Create its UI similar to an Activity
There are specialized fragments
Fragments have a lifecycle, just like Activities
Fragment Basics
17. Fragments Need Help
Must be hosted in an Activity
Activities are Not Directly Aware of
their Fragments
Fragments are aware of their host
Activity
Fragments are not aware of other
Fragments
FragmentManager is the middle man
19. Adding a Static Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
class="net.opgenorth.shakespeare.TitlesFragment"
android:id="@+id/titles_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
20. Adding a Dynamic Fragment
// Make new fragment to show this selection.
DetailsFragment details = new DetailsFragment();
// Execute a transaction, adding the fragment to the FrameLayout.
FragmentTransaction ft = FragmentManager.beginTransaction();
ft.add(Resource.Id.details, details, "details_fragment");
ft.commit();
23. Android Support Libraries
A set of code libraries that provide
backwards compatibility and features
to older API levels
24. Building a Dynamic UI with Fragments - http://developer.android.com/
training/basics/fragments/index.html
Support Libraries - http://developer.android.com/tools/support-library/
index.html
Links / References