SlideShare a Scribd company logo
Ch6. File, Saving States,
  and Preferences
           Browny
         30, May, 2011
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
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
Creating and Saving Preferences
•   Create or Modify a Shared Preference: call
    getSharedPreferences
•   To modify a Shared Preference use the
    SharedPreferences.Editor class
Retrieving Shared Preferences
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
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)
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)
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
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
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
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
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)
Finding/Using Shared Preferences


• The Shared Preference values recorded
  for the options presented in a Preference
  Activity are stored against the application
  Context
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
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
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
Saving Activity State (2/2)
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)
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
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
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
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
Thank you :)

More Related Content

PPTX
PPT
Shared preferences
PPTX
Java - Sockets
PPTX
Network programming in java - PPT
PPTX
Android share preferences
PPTX
Content provider in_android
PPTX
Program Threats
PDF
Android Location and Maps
Shared preferences
Java - Sockets
Network programming in java - PPT
Android share preferences
Content provider in_android
Program Threats
Android Location and Maps

What's hot (20)

PPTX
Share preference
PPTX
Android Layout.pptx
PPTX
Android User Interface
PDF
Action Bar in Android
PPTX
Java Beans
PPTX
Attributes of output primitives( curve attributes & area fill attributes)
PPTX
Servlets
PPT
3 d viewing
PPTX
Multi threading
PPTX
Android activity lifecycle
PDF
Servlet and servlet life cycle
PPT
Socket programming
PPT
Object oriented analysis
PPTX
sql function(ppt)
PPT
JAVA OOP
PDF
Java arrays
PPSX
JDBC: java DataBase connectivity
PDF
Android: Intent, Intent Filter, Broadcast Receivers
Share preference
Android Layout.pptx
Android User Interface
Action Bar in Android
Java Beans
Attributes of output primitives( curve attributes & area fill attributes)
Servlets
3 d viewing
Multi threading
Android activity lifecycle
Servlet and servlet life cycle
Socket programming
Object oriented analysis
sql function(ppt)
JAVA OOP
Java arrays
JDBC: java DataBase connectivity
Android: Intent, Intent Filter, Broadcast Receivers
Ad

Similar to Ch6 file, saving states, and preferences (20)

DOC
Data Storage
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PPTX
SHARED PREFERENCES IN ANDROID APPS.pptx
PDF
Android Data Persistence
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PPTX
Android Training (Storing & Shared Preferences)
PPTX
6CS027 Lecture 5 Files and Database Storage (1).pptx
PPTX
Tk2323 lecture 7 data storage
PPTX
Storage 8
PPT
Memory management
PDF
Lab4 - android
PDF
Data management
PDF
Data management
PPTX
Activity & Shared Preference
PDF
03 programmation mobile - android - (stockage, multithreads, web services)
DOCX
Android-data storage in android-chapter21
PPT
Data Storage In Android
PPT
MD-IV-CH-ppt.ppt
PDF
Android Development
PDF
[Android] Data Storage
Data Storage
Mobile Application Development -Lecture 11 & 12.pdf
SHARED PREFERENCES IN ANDROID APPS.pptx
Android Data Persistence
Mobile Application Development-Lecture 13 & 14.pdf
Android Training (Storing & Shared Preferences)
6CS027 Lecture 5 Files and Database Storage (1).pptx
Tk2323 lecture 7 data storage
Storage 8
Memory management
Lab4 - android
Data management
Data management
Activity & Shared Preference
03 programmation mobile - android - (stockage, multithreads, web services)
Android-data storage in android-chapter21
Data Storage In Android
MD-IV-CH-ppt.ppt
Android Development
[Android] Data Storage
Ad

More from Shih-Hsiang Lin (13)

PDF
Introduction to Apache Ant
PDF
Introduction to GNU Make Programming Language
PDF
[C++ gui programming with qt4] chap9
PDF
Ch5 intent broadcast receivers adapters and internet
PDF
Ch4 creating user interfaces
PDF
Ch3 creating application and activities
PDF
[C++ GUI Programming with Qt4] chap7
PPTX
[C++ GUI Programming with Qt4] chap4
PPTX
Function pointer
PPT
Introduction to homography
PPTX
Git basic
PPTX
Project Hosting by Google
PDF
An Introduction to Hidden Markov Model
Introduction to Apache Ant
Introduction to GNU Make Programming Language
[C++ gui programming with qt4] chap9
Ch5 intent broadcast receivers adapters and internet
Ch4 creating user interfaces
Ch3 creating application and activities
[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap4
Function pointer
Introduction to homography
Git basic
Project Hosting by Google
An Introduction to Hidden Markov Model

Recently uploaded (20)

PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Pre independence Education in Inndia.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
RMMM.pdf make it easy to upload and study
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
TR - Agricultural Crops Production NC III.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Business Ethics Teaching Materials for college
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
RMMM.pdf make it easy to upload and study
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Ch6 file, saving states, and preferences

  • 1. Ch6. File, Saving States, and Preferences Browny 30, May, 2011
  • 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
  • 16. Shared Preference Change Listeners (2/2)
  • 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

Editor's Notes