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. 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. Creating and Saving Preferences
• Create or Modify a Shared Preference: call
getSharedPreferences
• To modify a Shared Preference use the
SharedPreferences.Editor class
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. 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. 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. 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. 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. 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. 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. 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. Finding/Using Shared Preferences
• The Shared Preference values recorded
for the options presented in a Preference
Activity are stored against the application
Context
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
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
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. 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. 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. 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. 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