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_WRITABLEorContext.MODE_READABLEaccording to the javadoc. So I assume that you are talking aboutContext.MODE_WORLD_WRITABLEorContext.MODE_WORLD_READABLE. (Not that this is actually relevant to your question …)There is no functional difference.
Context.MODE_PRIVATEis anintconstant 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.