I have implemented Google and Facebook login in my android app. Can anyone please tell me how to store the user details in the app using shared preference for the same?
- 22,772
- 22
- 86
- 142
- 1,185
- 3
- 25
- 54
-
2Use Shared Preference – Rakshit Nawani Apr 11 '16 at 06:46
-
Using shared preferences you can store it see this http://www.tutorialspoint.com/android/android_shared_preferences.htm. – Jay Rathod Apr 11 '16 at 06:47
-
How to implement shared preference ? – Khadija Daruwala Apr 11 '16 at 06:48
-
1[Read the documentation](http://developer.android.com/guide/topics/data/data-storage.html). Try it. Ask about specific problems you encounter here. – Gerald Schneider Apr 11 '16 at 06:49
-
@GeraldSchneider Thanks ! – Khadija Daruwala Apr 11 '16 at 06:50
-
Possible duplicate of [Android Shared preferences example](http://stackoverflow.com/questions/23024831/android-shared-preferences-example) – Bajirao Shinde Apr 11 '16 at 07:10
3 Answers
Write to Shared Preferences
To write to a shared preferences file, create a SharedPreferences.Editor by calling edit() on your SharedPreferences.
Pass the keys and values you want to write with methods such as putInt() and putString(). Then call commit() to save the changes. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
Read from Shared Preferences
To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
check example Android Shared preferences example.
- 1
- 1
- 22,772
- 22
- 86
- 142
Setting Value in Shared
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "raks");
editor.putInt("idName", 1);
editor.commit();
Fetching Value from Shared
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
Here MY_PREFS_NAME is your File name
- 2,604
- 14
- 27
You can SharedPreferences to store it and check about user logging or not:
private void saveUser(Users user) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("userId", user.getUserId());
editor.putString("userName", user.getUserName());// or put anything you want in this with String type
editor.putString("registration_id", user.getRegistrationId());
editor.apply();
}
You should use editor.apply() than commit() because apply() will run in your background thread and do not make any conflit about it and main thread.
Then you can get values from it like this:
SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE);
int id = preferences.getInt("userId", 0);// 0 is default values
- 1,784
- 2
- 17
- 50