What I understood about context.MODE_PRIVATE or MODE_READABLE, WRITABLE is that those functions make files for sharedprefrences.
I am wondering what the difference is between context.getSharedPreferences(KEY, Context.MODE_PRIVATE) and getSharedPreferences(KEY, 0);.
getSharedPreferences retrieves its preferences from a xml folder as far as I know. And Context.MODE_PRIVATE stores its files. And why use context.getSharedPreferences(KEY, Context.MODE_PRIVATE) if both getSharedPreferences(KEY, 0) and context.getSharedPreferences(KEY, Context.MODE_PRIVATE) makes files.
Below is part of the Facebook API where I noticed Context.MODE_PRIVATE.
public static boolean save(Facebook session, Context context) {
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, session.getAccessToken());
editor.putLong(EXPIRES, session.getAccessExpires());
return editor.commit();
}
public static boolean restore(Facebook session, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
session.setAccessToken(savedSession.getString(TOKEN, null));
session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
return session.isSessionValid();
}
There is no Context.MODE_WRITABLE or Context.MODE_READABLE according to the javadoc. So I assume that you are talking about Context.MODE_WORLD_WRITABLE or Context.MODE_WORLD_READABLE. (Not that this is actually relevant to your question ...)
I am wondering what is the difference between
context.getSharedPreferences(KEY, Context.MODE_PRIVATE)and
context.getSharedPreferences(KEY, 0);
There is no functional difference. Context.MODE_PRIVATE is an int constant with value zero; refer to the javadoc linked above for the details. The former is more readable though, and that makes it preferable from a code style perspective.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With