Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 450
user3104719 Avatar asked Apr 12 '14 01:04

user3104719


People also ask

What is shared preference in Android with example?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

How do I pass data from one activity to another using SharedPreferences?

How to pass data from one activity to another in Android using shared preferences? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is the maximum number of shared preferences you can create?

there is no limit in Shared Preference.


2 Answers

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

like image 98
Jorgesys Avatar answered Oct 20 '22 11:10

Jorgesys


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 
like image 34
Krausz Lóránt Szilveszter Avatar answered Oct 20 '22 11:10

Krausz Lóránt Szilveszter