-3

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?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

3 Answers3

1

From http://developer.android.com/intl/es/training/basics/data-storage/shared-preferences.html#WriteSharedPreference

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.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

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

Rakshit Nawani
  • 2,604
  • 14
  • 27
0

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
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50