There are many ways of storing data in android, One of the simplest way is SharedPrefrences If you have a relatively small collection of key-values to save, you should use the SharedPrefrences APIs, Using SharedPrefrences You can stores: String, Integer, Float, Long, Double and a String Set.
download the full project from Github.
Save data to shared preferences
SharedPreferences sharedPreferences=getSharedPreferences("AppName",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("String","string");
editor.putBoolean("bool",true);
editor.putFloat("float",1.0f);
editor.putInt("int",1);
editor.putLong("long",1);
editor.apply();
Parameter:
“appName”
is name of the SharedPreference which will be useful when you read data from shared
preferences.
“MODE_PRIVATE”
By setting this mode, the file can only be accessed using by only your application.
“Stringkey”
is the Key for save and retrieve the value.
apply() or commit() to save the changes after putting data into the editor.
Difference between apply() and commit():
apply() write the data to disk asynchronously and commit write the data
synchronously We should avoid calling the commit() from main thread because
it could pause UI rendering.
Read from shared preferences
SharedPreferences sharedPref = getSharedPreferences("AppName",MODE_PRIVATE);
String stringValue=sharedPref.getString("string","");
Parameters:
StringKey is the key for getting the value.
optionally a default value to return if the key isn’t present.
Full code:
package c1c.sharedpreferenceexp;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Save the data in shared Preference.
SharedPreferences sharedPreferences=getSharedPreferences("AppName",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("bool",true);
editor.putFloat("float",1.0f);
editor.putInt("int",1);
editor.putLong("long",1);
editor.putString("String","string");
Set<String> set=new HashSet<String>();
set.add("element1");
set.add("element2");
set.add("element3");
set.add("element4");
editor.putStringSet("stringSet",set);
editor.apply();
//get the data from shared Prefernces.
SharedPreferences sharedPref = getSharedPreferences("AppName",MODE_PRIVATE);
boolean bool=sharedPref.getBoolean("bool",false);
Log.e("bool ",""+bool);
float floatValue=sharedPref.getFloat("float",0);
Log.e("floatValue ",""+floatValue);
int intValue=sharedPref.getInt("int",0);
Log.e("int ",""+intValue);
long longValue=sharedPref.getLong("long",0);
Log.e("long ",""+longValue);
String stringValue=sharedPref.getString("string","");
Log.e("stringValue ",""+stringValue);
Set<String> setvalue=sharedPref.getStringSet("stringSet",null);
Log.e("setValue ",""+setvalue);
}
}