Android - Fragments
Android - Fragments
Fragment
An activity is a container for views
When you have a larger screen device than a phone –like a
tablet it can look too simple to use phone interface here.
→ Fragments
◼ Mini-activities, each with its own set of views
◼ One or more fragments can be embedded in an Activity
◼ You can do this dynamically as a function of the device type (tablet or not) or
orientation
◼ ALSO, you can reuse fragments --- like reuse of mini interfaces
Fragment Idea
primarily to support more dynamic and
flexible UI designs on large screens, such as tablets.
→ Fragments
◼ Mini-activities, each with its own set of views
◼ One or more fragments can be embedded in an Activity
◼ You can do this dynamically as a function of the device type (tablet or not) or
orientation
You might
decide to run
a tablet in
portrait mode
with the handset
model of only
one fragment
in an Activity
Activity=
Fragment 2 Fragments
TextView Fragment 1
FragementTransaction: replace
Fragment 1 with Fragment 2
Activity
TextView Fragment 2
Fragment Lifecycle
Fragment in an Activity---Activity
Lifecyle influences
Activity paused → all its fragments paused
Activity destroyed → all its fragments are destroyed
Activity running → manipulate each fragment
independently.
Fragment transaction →add, remove,
etc.
adds it to a back stack that's managed by the activity—each
back stack entry in the activity is a record of the fragment
transaction that occurred.
The back stack allows the user to reverse a fragment
transaction (navigate backwards), by pressing the Back
button.
Fragment inside Activity
it lives in a ViewGroup inside the activity's view hierarchy
fragment has its own view layout.
via XML: Insert a fragment into your activity layout by
declaring the fragment in the activity's layout file, as a
<fragment> element,
via CODE: from your application code by adding it to an
existing ViewGroup.
you may also use a fragment without its own UI as an invisible
worker for the activity.
Fragment – extend a Fragment
class
via CODE: extend android.app.Fragment OR one
of its subclasses (DialogFragment, ListFragment,
PreferenceFragment, WebViewFragment )
IMPORTANT: must include a public empty constructor. The framework
will often re-instantiate a fragment class when needed, in particular during
state restore, and needs to be able to find this constructor to instantiate it. If
the empty constructor is not available, a runtime exception will occur in
some cases during state restore.
CALL Back functions (like Activity) : examples onCreate(), onStart(),
onPause(), and onStop().
Fragment methods (callback
functions)
onAttach(Activity) called once the fragment is associated with its
activity.
onCreate(Bundle) called to do initial creation of the fragment.
onCreateView(LayoutInflater, ViewGroup, Bundle) creates and
returns the view hierarchy associated with the fragment.
onActivityCreated(Bundle) tells the fragment that its activity has
completed its own Activity.onCreaate.
onStart() makes the fragment visible to the user (based on its
containing activity being started).
onResume() makes the fragment interacting with the user (based on
its containing activity being resumed).
Fragment methods (callback
functions)
As a fragment is no longer being used, it goes through a reverse series
of callbacks:
onPause() fragment is no longer interacting with the user either
ListFragment
Displays a list of items that are managed by an adapter (such as a
SimpleCursorAdapter), similar to ListActivity. It provides several methods for
managing a list view, such as the onListItemClick() callback to handle click events.
PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This
is useful when creating a "settings" activity for your application.
NOTE: sometimes there are multiple ways to achive something like a List –either with ListView
or ListFragment → they have different methods and lifecycles.
Fragments and their UI
--- how to specify the UI of a Fragment
Fragments and their UI
Most fragments will have a UI
Will have its own layout
you must implement the onCreateView() callback
method, which the Android system calls when it's
time for the fragment to draw its layout. Your
implementation of this method must return a View
that is the root of your fragment's layout.
◼ Note some subclasses of Fragment like ListFragment have
already implemented this method and you don’t need to
override.
Fragments and their UI –
onCreateView() using XML
Can implement onCreateView using XML
Pop fragments off the back stack, Fragment you put on it means
when user hits “back” you can
pop off from the BackStack a
popBackStack() (simulating a Back command by the user). Fragment to return to it
//Now you can call any methods you want on the details fragment
*******
FragmentTransaction
Used to replace, add and remove
DYNAMICALLY fragments
How to create FragmentTransaction
instance
You get it from the current FragmentManager –this
code is somewhere inside your Activity class
fragmentTransaction.addToBackStack(name);
As seen in previous slide lets you add transaction to BackStack (so can return
to previous fragment state when user hits “back”)
Example (repeated) ADDING
fragment dynamically
/*Inside Activity Code where you want to add Fragment (dynamically anywhere or in onCreate()
callback)
*/
/*Inside Activity Code where you want to add Fragment (dynamically anywhere or in onCreate() callback)
*/
// STEP 1: Create new fragment and create FragementTransaction from Manager
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
/ /Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
BackFragment achterkant= BackFragment.newInstance("blabla"); getFragmentManager() .beginTransaction()
If you want to get the fragment from the activity later, you need to use
findFragmentByTag().
For an example activity that uses a fragment as a background worker,
without a UI, see the FragmentRetainInstance.java sample at
https://android.googlesource.com/platform/development/+/master/samp
les/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstan
ce.java