0% found this document useful (0 votes)
20 views21 pages

Shared Preferences App Dev

The document provides an overview of SharedPreferences in Android, detailing its purpose as a lightweight data storage mechanism for application settings and user information. It explains how to access, write, and read data using various methods, and highlights the differences between the apply() and commit() methods for saving preferences. Additionally, it includes code examples for implementing SharedPreferences in an Android application.

Uploaded by

ferotoj759
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Shared Preferences App Dev

The document provides an overview of SharedPreferences in Android, detailing its purpose as a lightweight data storage mechanism for application settings and user information. It explains how to access, write, and read data using various methods, and highlights the differences between the apply() and commit() methods for saving preferences. Additionally, it includes code examples for implementing SharedPreferences in an Android application.

Uploaded by

ferotoj759
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SOFTWARES

FOR
SharedPreferences
MOBILEDEVICES
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
Todays Agenda

INTRODUCTION TO SHARED PREFERENCES INTRODUCTION TO


SHARED PREFERENCES IMPLEMENTATION FRAGMENTS

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
You are likely going to want to persistently store data

In Android, there are 5 main ways to store data:

• Shared Preferences – private key-value pair storage

Persistent • Internal Storage – private storage per-application


• External Storage – local shared storage (e.g., on SD card)
• SQLite Database – private structured data
Storage • Network Storage – store data on your own server

You should be able to make a decision on which


solution is best for various data in your application

Use multiple storage options!

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Data Storage in Android
Shared Standard Java File Content
I/O (plus Android SQLite Database
Preferences helper methods) Provider
• lightweight • save files • store • make private
data storage directly on the structured data available
mechanism device application to other
• primarily for • can use both data in a applications
saving internal and private • exposes
application external database read/write
settings and storage • full relational access to
user database application
information capability data

By Umar Naseer(Instructor - Department of


Computer Science, FAST NUCES CFD Campus)
Shared Preferences Overview
Has the user completed the
application settings? (boolean)
Good for basic data storage,
What is the user’s username?
simple examples: (string)

Shared Preferences accessed by boolean, float, int, long, string


string key, value can be:

Arbitrary objects cannot be Marshal to/from a private database


stored in Shared Preferences, (best)
best two options for arbitrary Marshal to/from binary files in
private storage
objects:
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
Shared Preferences Overview

Shared Preferences Android stores Shared The DATA folder can be


allows activities and Preferences settings as obtained by calling
applications to keep XML file in shared_prefs Environment.getDataDir
preferences, in the form folder under ectory().
of key-value pairs DATA/data/{application
similar to a Map that package} directory.
will persist even when /data/data/<package
the user closes the name>/shared_prefs/<pref
application. filename>.xml

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
SharedPreferences is application specific, i.e. the data
Shared is lost on performing one of the following options
Preferences on uninstalling the on clearing the application
Overview application data (through Settings)

As the name suggests, the primary purpose is to store


user-specified configuration details, such as user
specific settings, keeping the user logged into the
application.

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
• To get access to the preferences, we
have three APIs to choose from:
• getPreferences() : used from within
your Activity, to access activity-
specific preferences
Getting access • getSharedPreferences() : used
from within your Activity (or other
to shared application Context), to access
application-level preferences
preferences • getDefaultSharedPreferences() :
used on the PreferenceManager, to
get the shared preferences that
work in concert with Android’s
overall preference framework
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
Operating
Modes of
Shared
Preferences

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
To write shared preference values:
• Call edit() to get a SharedPreferences.Editor.
• Add values with editor “put” methods such as
putBoolean() and putString().
• Commit the new values with apply() or
Writing/Reading commit().
Shared To read shared preference values:
Preferences • Use SharedPreferences “get” methods such as
getBoolean() and getString().
• The “get” methods have two parameters
• the preference key string
• a default value to return if the preference is
undefined
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
Differences Between Methods
a p p l y ( ) and commit()
• commit()
• Returns a boolean value to indicate if the new values were successfully
written to persistent storage.
• Writes its preferences out to persistent storage synchronously (can block the
UI thread)
• apply()
• Commits its changes to the in-memory SharedPreferences immediately but
starts an asynchronous commit to disk.
• does not block the UI thread, but you won’t be notified of any failures.
Use a p p l y ( ) if you don't care about the return value
and you’re using this from your application’s UI thread.
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
/ / i n interface
SharedPreferences
boolean getBoolean(String
key, boolean defValue)
Selected Methods float getFloat(String
key, f l o a t defValue)
for int g e t I n t ( S t r i n g key,
Retrieving Shared i n t defValue)
Preferences long
long defValue)
getLong(String key,

String getString(String
key, S t r i n g defValue)
Set<String> g e t S t r i n g S e t ( S t r i ng
key, Set<String> defValues)
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
/ / i n i n t e r f a c e SharedPreferences.Editor
void a p p l y( )
boolean commit()
SharedPreferences.Editor putBoolean(String key,
Selected boolean value)
SharedPreferences.Editor p u t F l o a t ( S t r i n g key,

Methods for f l o a t value)


SharedPreferences.Editor p u t I n t ( S t r i n g key, i n t
value)
Saving Shared SharedPreferences.Editor putLong(String key,
long value)

Preferences SharedPreferences.Editor p u t S t r i n g ( S t r i n g key,


S t r i n g value)
SharedPreferences.Editor p u t St rin gSet(Stri ng
key,

Set<String> values)
SharedPreferences.Editor remove(String key)

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Initialization
• We need an editor to edit and save the changes in shared
preferences. The following code can be used to get the shared
preferences.

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Storing/Writing Data

editor.commit() is used in order to save changes to


shared preferences.
By Umar Naseer(Instructor - Departmentof
Computer Science, FAST NUCES CFD Campus)
Retrieving/Reading Data
Data can be retrieved from saved preferences by calling getString() as follows:

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Clearing or
Deleting
Data

• remove(“key_name”) is used to delete that particular value.


• clear() is used to remove all data

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Android Shared
Preferences A
simple application

By Umar Naseer(Instructor - Departmentof


Computer Science, FAST NUCES CFD Campus)
Code for
XML

By Umar Naseer(Instructor -
Departmentof
Computer Science,
FAST NUCES CFD
Campus)
Code for
Activity

By Muhammad Nouman Zafar


(Lecturer at Department of Computer
Science, Capital University of Science
and Technology, Islamabad, Pakistan)
Activity
Cont…

By Umar Naseer(Instructor -
Departmentof
Computer Science, FAST
NUCES CFD Campus)

You might also like