3 Fragments

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Romain

 Chiappinelli  
Tablets have larger displays than
phones do
They can support multiple UI panes /
user behaviors at the same time
The 1 activity – 1 thing the user can do
heuristic may not work for larger devices
Application uses two Activities
Ones shows titles of Shakespeare plays &
allows user to select one title
The other shows a quote from selected play
This layout is reasonable on a phone
But inefficient on a larger device
Use two cooperating layout units on one
screen
Represents a behavior / portion of
UI within an Activity
Multiple Fragments can be
embedded in an Activity to create
a multi-pane UI
A single Fragment can be reused
across multiple Activities
Fragment lifecycle is coordinated
with the lifecycle of its containing
Activity
Fragments have their own
lifecycles and receive their own
callbacks
Resumed
Fragment is visible in the running
activity
Paused
Another activity is in the foreground
and has focus, containing activity is
visible
Stopped
The fragment is not visible
Fragment is first onAttach()
attached to its
activity
Initialize the onAttach()
fragment

onCreate()
Fragment sets up & onAttach()
returns its user
interface
onCreate()

onCreateView()
Containing activity onAttach()
has completed
onCreate() and the
fragment has been onCreate()

installed
onCreateView()

onActivityCreated()
Hosting Activity onStart ()
about to become
visible
Hosting Activity is onResume()
about to become
visible and ready
for user interaction
Hosting Activity is onPause()
visible, but does not
have focus
Hosting Activity is onStop ()
no longer visible
View previously onDestroyView()
created by
onCreateView() has
been detached from
the Activity
Typical Actions
Clean up view
resources
Fragment is no onDestroyView()
longer in use
Typical Actions onDestroy()
Clean up Fragment
resources
Fragment no longer onDestroyView()
attached to its
activity
onDestroy()
Typical Actions
Null out references
to Hosting Activity onDetach()
Two general ways to add
Fragments to an Activity’s layout
Declare it statically in the
Activity’s layout file
Add it programmatically using the
fragmentManager
Layout can be inflated/
implemented in onCreateView()
onCreateView() must return the
View at the root of the Fragment’s
layout
This View is added to the containing
Activity
FragmentStaticLayout
FragmentStaticLayout
FragmentStaticLayout
While an Activity’s running you can
add a Fragment to it’s layout
Get reference to the
FragmentManager
Begin a FragmentTransaction
Add the Fragment
Commit the FragmentTransaction
FragmentProgrammaticLayout
FragmentProgrammaticLayout
Fragment transactions allow you
to dynamically change your app’s
user interface
Can make the interface more fluid
& take better advantage of
available screen space
FragmentDynamicLayout
FragmentDynamicLayout
If you call setRetainInstance(true),
Android won’t destroy the Fragment
on configuration changes
Results in some changes to lifecycle
callback sequence
onDestroy() will not be called
onCreate() will not be called
Functionality is similar to previous
examples
In landscape mode
Both Fragments use a large font
TitleFragment takes more
horizontal space & allows long
titles to span multiple lines
In portrait mode
Both Fragments use a smaller
font
TitleFragment will use less space
and will ellipsize long titles,
limiting them to a single line
FragmentDynamicLayout
FragmentDynamicLayout
User Interface classes

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy