Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between preference and shared preference in Android

Tags:

The concept of preferences and shared preferences in Android are mixed up for me. What is the difference?

like image 587
Nouman Ghaffar Avatar asked Apr 22 '14 14:04

Nouman Ghaffar


People also ask

What is the difference between getPreferences () and getSharedPreferences ()?

getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app. getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity.

What is shared preference in Android?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

What is the difference between SQLite and SharedPreferences in Android?

Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.

Can we use SharedPreferences in Android?

Use SharedPreferences in Android if you want to store simple data like name, phone number, age, email, last opened screen... We are going to talk about SharedPreferences in Android. It is a place where you can store data; this is an XML file with values stored in it.


1 Answers

Preferences: The user interface part of the settings. It contains different classes which allow to composes Settings screens from code or XML. They can look like this:

Image

Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you. They are not encrypted and can easily be changed when the user has rooted his/her phone (nice for development). Don't use these for sensitive information. The above mentioned Preferences use Shared Preferences as the underlying system.

like image 137
tilpner Avatar answered Oct 19 '22 15:10

tilpner