Why use Context.MODE_PRIVATE or Context.MODE_WRITABLE?

What I understood about context.MODE_PRIVATE or MODE_READABLE, WRITABLE , is that these 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 preferences from the xml folder as far as I know. And context.MODE_PRIVATE stores its files. And why use context.getSharedPreferences(KEY, Context.MODE_PRIVATE) if the getSharedPreferences(KEY, 0) and context.getSharedPreferences(KEY, Context.MODE_PRIVATE) files create.

Below is the 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(); } 
+6
source share
1 answer

No Context.MODE_WRITABLE or Context.MODE_READABLE according to javadoc . Therefore, I assume that you are talking about Context.MODE_WORLD_WRITABLE or Context.MODE_WORLD_READABLE . (Not that it really related to your question ...)


I wonder 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 a value of 0; refer to javadoc above for details. The first one is more readable, and this makes it preferable in terms of code style.

+7
source

Source: https://habr.com/ru/post/904867/


All Articles