-2

If I have a string, how can I locally save it into the user's device? I don't mind it being in a file. If it is easier using Parse.com, please show me how. The string I want to save:

    final String userIDString = user.getObjectId().toString();

Please help me. I am using parse to get a user's object ID, so don't worry about the string. Just tell me how to save a string locally into a phone. Thanks.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

1 Answers1

15

You can use SharedPreferences:

public class MainActivity extends Activity {
    ...
    // initialize SharedPreferences var
    SharedPreferences sharedPref;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        // get or create SharedPreferences 
        sharedPref = getSharedPreferences("myPref", MODE_PRIVATE);

        // save your string in SharedPreferences
        sharedPref.edit().putString("user_id", userIDString).commit();
    }
}

And for getting your String from SharedPreferences:

String userId = sharedPref.getString("user_id", "default if empty");

See more about data storage using SharedPreferences here

leonheess
  • 16,068
  • 14
  • 77
  • 112
Seyyed
  • 1,526
  • 3
  • 20
  • 30