Android Shared preferences for creating one time activity (example) [closed]
I have three activities A,B and C where A and B are forms and after filling and saving the form data in database(SQLITE). I am using intent from A to B and then B to C.What i want is that every time I open my app I want C as my home screen and not A and B anymore.
I guess shared preferences would work for this, but I cannot find a good example to give me a starting place. Any help would be appreciated.
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
Retrieve data from preference:
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.
More info:
Using Shared Preferences
Shared Preferences
Create SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
Storing data as KEY/VALUE pair
editor.putBoolean("key_name1", true); // Saving boolean - true/false
editor.putInt("key_name2", "int value"); // Saving integer
editor.putFloat("key_name3", "float value"); // Saving float
editor.putLong("key_name4", "long value"); // Saving long
editor.putString("key_name5", "string value"); // Saving string
// Save the changes in SharedPreferences
editor.apply(); // commit changes
Get SharedPreferences data
// If value for key not exist then return second param value - In this case null
boolean userFirstLogin= pref.getBoolean("key_name1", true); // getting boolean
int pageNumber=pref.getInt("key_name2", 0); // getting Integer
float amount=pref.getFloat("key_name3", null); // getting Float
long distance=pref.getLong("key_name4", null); // getting Long
String email=pref.getString("key_name5", null); // getting String
Deleting Key value from SharedPreferences
editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4
// Save the changes in SharedPreferences
editor.apply(); // commit changes
Clear all data from SharedPreferences
editor.clear();
editor.apply(); // commit changes
How to Intialize?
// 0 - for private mode`
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
How to Store Data In Shared Preference?
editor.putString("key_name", "string value"); // Storing string
OR
editor.putInt("key_name", "int value"); //Storing integer
And don't forget to apply :
editor.apply();
How to retrieve Data From Shared Preferences ?
pref.getString("key_name", null); // getting String
pref.getInt("key_name", 0); // getting Integer
Hope this will Help U :)
You can create your custom SharedPreference class
public class YourPreference {
private static YourPreference yourPreference;
private SharedPreferences sharedPreferences;
public static YourPreference getInstance(Context context) {
if (yourPreference == null) {
yourPreference = new YourPreference(context);
}
return yourPreference;
}
private YourPreference(Context context) {
sharedPreferences = context.getSharedPreferences("YourCustomNamedPreference",Context.MODE_PRIVATE);
}
public void saveData(String key,String value) {
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor .putString(key, value);
prefsEditor.commit();
}
public String getData(String key) {
if (sharedPreferences!= null) {
return sharedPreferences.getString(key, "");
}
return "";
}
}
You can get YourPrefrence instance like:
YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.saveData(YOUR_KEY,YOUR_VALUE);
String value = yourPreference.getData(YOUR_KEY);
I just found all the above examples just too confusing, so I wrote my own. Code fragments are fine if you know what you're doing, but what about people like me who don't?
Want a cut-n-paste solution instead? Well here it is!
Create a new java file and call it Keystore. Then paste in this code:
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class Keystore { //Did you remember to vote up my example?
private static Keystore store;
private SharedPreferences SP;
private static String filename="Keys";
private Keystore(Context context) {
SP = context.getApplicationContext().getSharedPreferences(filename,0);
}
public static Keystore getInstance(Context context) {
if (store == null) {
Log.v("Keystore","NEW STORE");
store = new Keystore(context);
}
return store;
}
public void put(String key, String value) {//Log.v("Keystore","PUT "+key+" "+value);
Editor editor = SP.edit();
editor.putString(key, value);
editor.commit(); // Stop everything and do an immediate save!
// editor.apply();//Keep going and save when you are not busy - Available only in APIs 9 and above. This is the preferred way of saving.
}
public String get(String key) {//Log.v("Keystore","GET from "+key);
return SP.getString(key, null);
}
public int getInt(String key) {//Log.v("Keystore","GET INT from "+key);
return SP.getInt(key, 0);
}
public void putInt(String key, int num) {//Log.v("Keystore","PUT INT "+key+" "+String.valueOf(num));
Editor editor = SP.edit();
editor.putInt(key, num);
editor.commit();
}
public void clear(){ // Delete all shared preferences
Editor editor = SP.edit();
editor.clear();
editor.commit();
}
public void remove(){ // Delete only the shared preference that you want
Editor editor = SP.edit();
editor.remove(filename);
editor.commit();
}
}
Now save that file and forget about it. You're done with it. Now go back into your activity and use it like this:
public class YourClass extends Activity{
private Keystore store;//Holds our key pairs
public YourSub(Context context){
store = Keystore.getInstance(context);//Creates or Gets our key pairs. You MUST have access to current context!
int= store.getInt("key name to get int value");
string = store.get("key name to get string value");
store.putInt("key name to store int value",int_var);
store.put("key name to store string value",string_var);
}
}