Question: I have a user login app, which takes email, and password from user. I want to save the email and password in phone cache so user does not have to type again. Is there any way to save data in Android cache.

Answer: 
SharedPreferences for this kind of type are **much** simpler than use application cache.

For more info about SharedPreferences read documentation, you can have SharedPreferences related to an activity or to an activity (it changes just where it will be saved.)

To access to the default sharedpreference you will use the static method of `PreferenceManager` named `getDefaultSharedPreferece` which takes as argument the context (`this` if you are inside an Activity)

```
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

```

Now using this SharedPreferences instance you can read everything which is saved inside this file.

To read you will use the methods sharedPreferences.get(String/Int/Long/Float etc. etc.).

Example to read the email and password of the user, you could write

```
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String email = sharedPreferences.getString("email", null);
String password = sharedPreferences.getString("password", null");

```

> 
> ("password", null)
> 
> 
> 

1. "password": will be the key, in fact shared preferences are saved as `key=value` pair, so you should use the same key when you read and when you write to be sure you update/read the same value.
2. null: It's the defaultValue, the variable `password` will be `null` if Android failed to found the key inside the file.

Right now, if you execute this code email and password will be `null` since you didn't add nothing.

To add something in the file you should use `SharedPreferences.Editor` which contains all methods to edit key-value pairs. To create this object you will call method `edit()` in our sharedPreference variable.

```
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();

```

now we will use our `editor` variable to save our data.

Like read, it has methods like put(String/Int/Long) (the same of read just with put)

So we want to save email and password of the user, since are strings we will use `putString`

```
editor.putString("password", "helloWorld");

```

It's similar to what we have see above, but with a difference: While `password` is still the key to use (note: it's the same of the read line) the second argument is the **value**, as the name says it's what we should write as password. It this case it will save `helloWorld` as password.

The same for email, replace `helloWorld` with your variable which contains the password.

Remember, you **should** call `.commit()` (of editor) when you are ready to save values in the file. (if you don't, you will not update the values inside the file.)

Your code will look like this:

```
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("email", "helloWorld@gmail.com");
editor.putString("password", "helloWorld");
editor.commit();

```

Now, when you execute again the read code you will read in the email `helloWorld@gmail.com` and in password `helloWorld`.
Use [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) for this. Something like [this](http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/)