Ch6. File, Saving States,  and Preferences           Browny         30, May, 2011
Outline• Saving Simple Application Data• Creating and Saving Preferences• Retrieving Shared Preferences• Preference Activi...
Saving Simple Application Data• 3 data-persistence techniques ‣   Shared
Preferences
(key/value
pairs,
can
be
     shared
...
Creating and Saving Preferences•   Create or Modify a Shared Preference: call    getSharedPreferences•   To modify a Share...
Retrieving Shared Preferences
Preference Activity and Framework               (1/2)• Android offers an XML-driven framework  to create system-style pref...
Preference Activity and Framework               (2/2)• 3 parts of Preference Activity Framework ‣   Preference
Screen
Layo...
Preference Screen Layout (1/2)• Stored in the res/xml resources folder• Nested Preference Screen elements (each  will be r...
Preference Screen Layout (2/2)• The specific attributes available for each  preference, at least the following 4 ‣   androi...
Native Preference Controls• CheckBoxPreference• EditTextPreference• ListPreference (equivalent of a spinner)• RingtonePref...
Using Intents to Import System     Preference Screens (1/2)• Preference hierarchies can include  Preference Screens from o...
Using Intents to Import System    Preference Screens (2/2)• Make your own Preference Screens  available for invocation usi...
Preference Activity•   Host the preference hierarchy defined using the    preferences XML file (extend the    PreferenceActi...
Finding/Using Shared Preferences• The Shared Preference values recorded  for the options presented in a Preference  Activi...
Shared Preference Change Listeners               (1/2)• A callback  (onSharedPreferenceChangeListener)  whenever a particu...
Shared Preference Change Listeners               (2/2)
Saving Activity State (1/2)•   When:    ‣   Want
to
save
Ac8vity
informa8on
that
doesn’t
need
to
        be
shared
with
ot...
Saving Activity State (2/2)
Saving and Restoring Instance State               (1/2)• Saving (onSaveInstanceState event handler) ‣   Be
triggered
whene...
Saving and Restoring Instance State               (2/2)• Restoring ‣   The
saved
Bundle
is
passed
in
to
the
     onRestore...
Saving and Loading Files•   Android offers openFileInput and openFileOuput to simplify    reading and writing streams from...
Including Static Files as Resources • Application requires external file resources,   include them in your distribution pac...
File Management Tools• Android supply some specialized utilities for  file management that are available from the  applicat...
Thank you :)
Upcoming SlideShare
Loading in …5
×

Ch6 file, saving states, and preferences

1,288 views
1,107 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,288
On SlideShare
0
From Embeds
0
Number of Embeds
72
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Ch6 file, saving states, and preferences

    1. 1. Ch6. File, Saving States, and Preferences Browny 30, May, 2011
    2. 2. Outline• Saving Simple Application Data• Creating and Saving Preferences• Retrieving Shared Preferences• Preference Activity and Framework• Saving Activity State• Saving and Loading Files• Including Static File as Resources• File Management Tools
    3. 3. Saving Simple Application Data• 3 data-persistence techniques ‣ Shared
Preferences
(key/value
pairs,
can
be
 shared
among
applica8on
components
in
the
 same
applica8on
context,

but
not
available
to
 other
applica8ons) ‣ Saved
Applica2on
data
(A
pair
of
event
handlers
 used
for
saving
Ac8vity
instance
details) ‣ Files
    4. 4. Creating and Saving Preferences• Create or Modify a Shared Preference: call getSharedPreferences• To modify a Shared Preference use the SharedPreferences.Editor class
    5. 5. Retrieving Shared Preferences
    6. 6. Preference Activity and Framework (1/2)• Android offers an XML-driven framework to create system-style preference screens for your applications• 2 advantages ‣ Familiarity ‣ Integrate
se@ngs
screens
from
other
 applica8ons
into
your
applica8on’s
se@ngs
 screens
    7. 7. Preference Activity and Framework (2/2)• 3 parts of Preference Activity Framework ‣ Preference
Screen
Layout
(An
XML
file
that
 defines
the
hierarchy
displayed
in
your
 Preference
Ac8vity) ‣ Preference
Ac2vity
(An
extension
of
 PreferenceAc(vity
that
will
be
used
to
host
your
 applica8on
preference
screens) ‣ Shared
Preference
Change
Listener
(An
 implementa8on
of
the
 onSharedPreferenceChangeListener
class)
    8. 8. Preference Screen Layout (1/2)• Stored in the res/xml resources folder• Nested Preference Screen elements (each will be represented as a selectable element that will display a new screen if clicked)
    9. 9. Preference Screen Layout (2/2)• The specific attributes available for each preference, at least the following 4 ‣ android:key,
android:+tle,
android:summary,
 android:defaultValue
    10. 10. Native Preference Controls• CheckBoxPreference• EditTextPreference• ListPreference (equivalent of a spinner)• RingtonePreference (useful when you’re constructing a screen to configure notification settings)• Extending the Preference class
    11. 11. Using Intents to Import System Preference Screens (1/2)• Preference hierarchies can include Preference Screens from other applications (including system Preference Screens)• Useful for including links to relevant system Preference Screens within your own application settings
    12. 12. Using Intents to Import System Preference Screens (2/2)• Make your own Preference Screens available for invocation using intent, simply add an Intent Filter to the manifest entry for the host Preference Activity
    13. 13. Preference Activity• Host the preference hierarchy defined using the preferences XML file (extend the PreferenceActivity)• To display the application settings hosted in this Activity (call startActivity or startActivityForResult)
    14. 14. Finding/Using Shared Preferences• The Shared Preference values recorded for the options presented in a Preference Activity are stored against the application Context
    15. 15. Shared Preference Change Listeners (1/2)• A callback (onSharedPreferenceChangeListener) whenever a particular Shared Preference value is added, removed, or modified• Using this handler your application components can listen for changes to user preferences and update their UIs or behavior as required
    16. 16. Shared Preference Change Listeners (2/2)
    17. 17. Saving Activity State (1/2)• When: ‣ Want
to
save
Ac8vity
informa8on
that
doesn’t
need
to
 be
shared
with
other
components• How ‣ Call
Ac(vity.getPreferences()
without
specifying
a
 Shared
Preferences
name• What ‣ Access
to
the
returned
Shared
Preferences
map
is
 restricted
to
the
calling
Ac8vity;
each
Ac8vity
supports
 a
single
unnamed
Shared
Preferences
object
    18. 18. Saving Activity State (2/2)
    19. 19. Saving and Restoring Instance State (1/2)• Saving (onSaveInstanceState event handler) ‣ Be
triggered
whenever
an
Ac8vity
completes
its
 ac8ve
lifecycle.
Only
when
it’s
not
being
 explicitly
finished
(with
a
call
to
finish)
    20. 20. Saving and Restoring Instance State (2/2)• Restoring ‣ The
saved
Bundle
is
passed
in
to
the
 onRestoreInstanceState
and
onCreate
methods
if
 the
applica8on
is
forced
to
restart
during
a
session
    21. 21. Saving and Loading Files• Android offers openFileInput and openFileOuput to simplify reading and writing streams from and to local files• Support only those files in the current application folder; specifying path separators will cause an exception to be thrown• Files created using the openFileOuput method are private to the calling application
    22. 22. Including Static Files as Resources • Application requires external file resources, include them in your distribution package by placing them in the res/raw folder of your project hierarchy• Android’s resource mechanism lets you specify alternative resource files for different languages, locations, and hardware configurations
    23. 23. File Management Tools• Android supply some specialized utilities for file management that are available from the application Context ‣ deleteFile
(Enables
you
to
remove
files
created
by
 the
current
applica8on) ‣ fileList
(Returns
a
string
array
that
includes
all
the
 files
created
by
the
current
applica8on)• Useful for cleaning up temporary files left behind if your application crashes or is killed unexpectedly
    24. 24. Thank you :)

    ×