Android Shared preferences for creating one time activity (example)

user3104719 picture user3104719 · Apr 12, 2014 · Viewed 527.4k times · Source

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.

Answer

Jorgesys picture Jorgesys · Apr 12, 2014

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