0

In the official site of Android, for getPreference() method, it is said that :

Retrieve a SharedPreferences object for accessing preferences that are private to this activity.

And here(How do I get the SharedPreferences from a PreferenceActivity in Android?) it is said that :

These preferences can only be used within the particular activity and can not be used by other components of the application.

However, in url http://skillgun.com/android/basics/interview-questions-and-answers/paper/25 5th question,

it is said that it is not guaranteed to be protected as it will be stored with the name of Activity.

I am confused whether other components of an application(such as activity,service etc.) can access the shared preference created by getPreference() method. is a shared preference created by calling getPreference() method accessible only for an activity for all circumstances?

Community
  • 1
  • 1
oiyio
  • 5,219
  • 4
  • 42
  • 54
  • That last quote - `getPreferences()` likely delegates to the `getSharedPreferences()` method with the current Activity name. Since the activity name is simply a string, it isn't protected – OneCricketeer Nov 03 '16 at 13:49
  • @cricket_007 why it is said "private to this activity" in android official document. It leads misunderstanding.. – oiyio Nov 04 '16 at 06:44

1 Answers1

0

Basically if you use shared preferences you will be able to read and write the preference from any part of your app. But other apps will not be able to access this information.

The statement regarding not being protected refers to the fact that rooted users (and apps) can read this files from the phone internal storage. So avoid at any cost saving sensitive user information in Shared Preferences. Ex. Dont store userNames, Passwords, personal details, etc.

Use shared preferences for simple things you wish to store Ex. If developing a contacts app you can store whether the user likes to read his contacts firstName LastName or LastName FirstName.

This kind of data is very short and not compromising.

If you require to store sensitive information always encrypt the data first.

Graham
  • 7,431
  • 18
  • 59
  • 84
LordSidious
  • 508
  • 5
  • 15