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 :)
Ad

More Related Content

What's hot (20)

Android share preferences
Android share preferencesAndroid share preferences
Android share preferences
Ajay Panchal
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
Krazy Koder
 
Fragment
Fragment Fragment
Fragment
nationalmobileapps
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
Shakib Hasan Sumon
 
Android UI
Android UIAndroid UI
Android UI
nationalmobileapps
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
Nakka Srilakshmi
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
Kan-Han (John) Lu
 
android sqlite
android sqliteandroid sqlite
android sqlite
Deepa Rani
 
Android application structure
Android application structureAndroid application structure
Android application structure
Alexey Ustenko
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
nationalmobileapps
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
CodeAndroid
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
android activity
android activityandroid activity
android activity
Deepa Rani
 

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

TechNet Live spor 1 sesjon 2 - sc-forefront 2
TechNet Live spor 1   sesjon 2 - sc-forefront 2TechNet Live spor 1   sesjon 2 - sc-forefront 2
TechNet Live spor 1 sesjon 2 - sc-forefront 2
Anders Borchsenius
 
LCM JB.pptx
LCM JB.pptxLCM JB.pptx
LCM JB.pptx
ssuser4dcfdf
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behestee
Hussain Behestee
 
Mobile Application Development (Shared Preferences) class-06
Mobile Application Development (Shared Preferences) class-06Mobile Application Development (Shared Preferences) class-06
Mobile Application Development (Shared Preferences) class-06
Dr. Mazin Mohamed alkathiri
 
Android Penetration Testing - Day 1
Android Penetration Testing - Day 1Android Penetration Testing - Day 1
Android Penetration Testing - Day 1
Mohammed Adam
 
Module 3: Working with Jazz Source Control
Module 3: Working with Jazz Source ControlModule 3: Working with Jazz Source Control
Module 3: Working with Jazz Source Control
IBM Rational software
 
Community Tenure Recording Training Material Web Admin – User Training
Community Tenure Recording Training Material  Web Admin – User Training Community Tenure Recording Training Material  Web Admin – User Training
Community Tenure Recording Training Material Web Admin – User Training
FAO
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architectureAndroid Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
NicheTech Com. Solutions Pvt. Ltd.
 
Using the Corporate Geographic Data Model with Subversion
Using the Corporate Geographic Data Model with SubversionUsing the Corporate Geographic Data Model with Subversion
Using the Corporate Geographic Data Model with Subversion
Debbie Wilson
 
R12 d49656 gc10-apps dba 16
R12 d49656 gc10-apps dba 16R12 d49656 gc10-apps dba 16
R12 d49656 gc10-apps dba 16
zeesniper
 
Cetas - Application Development Services
Cetas - Application Development ServicesCetas - Application Development Services
Cetas - Application Development Services
Kabilan D
 
Vmware thin app architecture
Vmware thin app architectureVmware thin app architecture
Vmware thin app architecture
solarisyougood
 
Session 1
Session 1Session 1
Session 1
Anwar Yahyawi
 
Mobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdfMobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdf
AbdullahMunir32
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
application developer
 
Memory management
Memory managementMemory management
Memory management
Vikash Patel
 
this is PPT for mobail application development
this is PPT for mobail application developmentthis is PPT for mobail application development
this is PPT for mobail application development
swapnildhage339
 
Storage 8
Storage   8Storage   8
Storage 8
Michael Shrove
 
Mobile Application Development -Lecture 11 & 12.pdf
Mobile Application Development -Lecture 11 & 12.pdfMobile Application Development -Lecture 11 & 12.pdf
Mobile Application Development -Lecture 11 & 12.pdf
AbdullahMunir32
 
Angular meteor presentation
Angular meteor presentationAngular meteor presentation
Angular meteor presentation
Jean Marcel Belmont
 
TechNet Live spor 1 sesjon 2 - sc-forefront 2
TechNet Live spor 1   sesjon 2 - sc-forefront 2TechNet Live spor 1   sesjon 2 - sc-forefront 2
TechNet Live spor 1 sesjon 2 - sc-forefront 2
Anders Borchsenius
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behestee
Hussain Behestee
 
Mobile Application Development (Shared Preferences) class-06
Mobile Application Development (Shared Preferences) class-06Mobile Application Development (Shared Preferences) class-06
Mobile Application Development (Shared Preferences) class-06
Dr. Mazin Mohamed alkathiri
 
Android Penetration Testing - Day 1
Android Penetration Testing - Day 1Android Penetration Testing - Day 1
Android Penetration Testing - Day 1
Mohammed Adam
 
Module 3: Working with Jazz Source Control
Module 3: Working with Jazz Source ControlModule 3: Working with Jazz Source Control
Module 3: Working with Jazz Source Control
IBM Rational software
 
Community Tenure Recording Training Material Web Admin – User Training
Community Tenure Recording Training Material  Web Admin – User Training Community Tenure Recording Training Material  Web Admin – User Training
Community Tenure Recording Training Material Web Admin – User Training
FAO
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architectureAndroid Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
NicheTech Com. Solutions Pvt. Ltd.
 
Using the Corporate Geographic Data Model with Subversion
Using the Corporate Geographic Data Model with SubversionUsing the Corporate Geographic Data Model with Subversion
Using the Corporate Geographic Data Model with Subversion
Debbie Wilson
 
R12 d49656 gc10-apps dba 16
R12 d49656 gc10-apps dba 16R12 d49656 gc10-apps dba 16
R12 d49656 gc10-apps dba 16
zeesniper
 
Cetas - Application Development Services
Cetas - Application Development ServicesCetas - Application Development Services
Cetas - Application Development Services
Kabilan D
 
Vmware thin app architecture
Vmware thin app architectureVmware thin app architecture
Vmware thin app architecture
solarisyougood
 
Mobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdfMobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdf
AbdullahMunir32
 
this is PPT for mobail application development
this is PPT for mobail application developmentthis is PPT for mobail application development
this is PPT for mobail application development
swapnildhage339
 
Mobile Application Development -Lecture 11 & 12.pdf
Mobile Application Development -Lecture 11 & 12.pdfMobile Application Development -Lecture 11 & 12.pdf
Mobile Application Development -Lecture 11 & 12.pdf
AbdullahMunir32
 
Ad

More from Shih-Hsiang Lin (13)

Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache Ant
Shih-Hsiang Lin
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
Shih-Hsiang Lin
 
[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9
Shih-Hsiang Lin
 
Ch5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetCh5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internet
Shih-Hsiang Lin
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfaces
Shih-Hsiang Lin
 
Ch3 creating application and activities
Ch3 creating application and activitiesCh3 creating application and activities
Ch3 creating application and activities
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4
Shih-Hsiang Lin
 
Function pointer
Function pointerFunction pointer
Function pointer
Shih-Hsiang Lin
 
Introduction to homography
Introduction to homographyIntroduction to homography
Introduction to homography
Shih-Hsiang Lin
 
Git basic
Git basicGit basic
Git basic
Shih-Hsiang Lin
 
Project Hosting by Google
Project Hosting by GoogleProject Hosting by Google
Project Hosting by Google
Shih-Hsiang Lin
 
An Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelAn Introduction to Hidden Markov Model
An Introduction to Hidden Markov Model
Shih-Hsiang Lin
 
Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache Ant
Shih-Hsiang Lin
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
Shih-Hsiang Lin
 
[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9
Shih-Hsiang Lin
 
Ch5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetCh5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internet
Shih-Hsiang Lin
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfaces
Shih-Hsiang Lin
 
Ch3 creating application and activities
Ch3 creating application and activitiesCh3 creating application and activities
Ch3 creating application and activities
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4
Shih-Hsiang Lin
 
Introduction to homography
Introduction to homographyIntroduction to homography
Introduction to homography
Shih-Hsiang Lin
 
Project Hosting by Google
Project Hosting by GoogleProject Hosting by Google
Project Hosting by Google
Shih-Hsiang Lin
 
An Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelAn Introduction to Hidden Markov Model
An Introduction to Hidden Markov Model
Shih-Hsiang Lin
 
Ad

Recently uploaded (20)

Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 

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