String getString(String name, String defValue){...}
This is the definition of getString(...) method of SharedPreferences so I think it's possible if I run code below, it returns 1 two times:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Timber.e(pref.getString("new", "1"));
Timber.e(pref.getString("new", "100"));
because at the first time its empty, so "1" will store, after that because of it has value ("1"), it will return it's value ("1") not default value ("100")
but it returns "1" and "100" and "new" does not store in my pref file (located in data/data/...)
Am I understanding it wrong or something goes wrong in this code?
Use can only get The data using getString.
to store data use Editor.commit();
Editor editor = settings.edit();
editor.putString("someKey", "someVal");
editor.commit();
only after that you can get this value.
String value = settings.getString("someKey", "someDefaultValueIfThisKeyNotUsedBefore");
In this example, you will recieve "someVal" if commit is used beforehand.
You must put"Something" to SharedPreferences.Editor, and commit them.
pref.getString wont store anything.
Refer to the links below:
https://developer.android.com/reference/android/content/SharedPreferences.html
https://developer.android.com/reference/android/content/SharedPreferences.Editor.html
Related
I am using Android Studio and programming in Kotlin. I haven't been able to find a way to save the data entered by the user so it appears when they reopen the app. Thank you ahead of time!
You have to store your data in shared preference when any click event listen or on edittext textwatcher you can set your character in preference but best way is on any click event save data on preference and when activity open set data from preference on edittext.
Store it in Shared Preferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
use this save the value..
Editor editor = sharedpreferences.edit();
editor.putString("key_name", "value"); //here give name and value to save
editor.commit();
use this to get the value.
String edittextvalue = pref.getString("key_name", null); //put edittextvalue where ever you what to show the value.
use this step to remove or clear the value
editor.remove("name");
editor.commit();
This question already has answers here:
Shared preferences for creating one time activity
(14 answers)
Closed 4 years ago.
I read about SharedPreferences but did not understand where i need to put the saving data and where to put the get objects.
In my app i get the full name when i open it in the first time by dialog.
I need to save the full name for ever (until the user will delete the app or something).
Where and what should i write to save the data(in onDestroy)?
Like :
// Create object of SharedPreferences.
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
//now get Editor
SharedPreferences.Editor editor= sharedPref.edit();
//put your value
editor.putString("name", strName);
editor.commit();
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
And where and what should i write to get the data(in onCreate)?
You don't have to to anything in onDestroy(). If your app gets uninstalled your data in shared preferences will be removed as well.
editor.putString("name", strName);
The first parameter is the key and the second one ist the value.
if you want to save the user's name, you pass the first parameter "name" and for the second parameter, the user's name.
when you want to read the user's name later you use
String name = sharedPref.getString("name", "");
Again, the first parameter is the key. You want to read the user's name so you use "name" and the second parameter is the default value, if there hasn't been saved a value, yet.
I tried to append the data in shared preference file by using
SharedPreferences sharedPreferences = getSharedPreferences("myData", MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", userName.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
But I found that new value overwrites the old value. Will you help me to fix this issue?
MODE_APPEND doesn't mean that you add multiple values for each key. It means that if the file already exists it is appended to and not erased . We usually used MODE_PRIVATE.
As for saving multiple names and passwords, you can take a look at putStringSet(string key Set<String> values Method.
You can save the for each key a set of string values. You can separate the username and password by some special character or string. You may even serialize an object to json.
So basically what you need to do is:
Get the list of values from Shared Preferences
Append the current value to the list.
Save the List back to Shared Preferences.
I am Wondering about One-time screens... I know, I should use something like SharedPreferences or stuff like that.
If someone has a simple solution for one-time login screen. And a little example.
My login contains: weight, name , height, age and gender (spinner)
You can take a look at Android User info and Sign in :
https://developer.android.com/training/sign-in/index.html
Or you can use login with Facebook API.
Otherwise, I would use Shared prefs.
Create a shared prefs file
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
This will create a private file for the current activity. You can use MODE_WORLD_READABLE and MODE_WORLD_WRITABLE if it fits your needs.
You can also provide a file name as the first parameter if needed :
SharedPreferences sharedPreferences = getPreferences("com.example.stackoverflow.myfile", Context.MODE_PRIVATE);
Write a shared pref
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("USERNAME", "test");
editor.commit();
You can put any primitive type : int, string, boolean, etc.
It is a key/value set. the key string "USERNAME" will then have a value of "test".
Read shared pref
String username = sharedPreferences.getString("USERNAME", "NO NAME");
The second parameter is a default value to use if the key "USERNAME" didn't get any value.
In my first activity I am saving my shared preference as
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
settings.edit().putString("uname", username);
Then I am reading it in another activity as
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
String uname = settings.getString("uname", "");
But the uname string is always empty any suggestions
You forgot to do .commit
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#commit%28%29
settings.edit().putString("uname", username).commit();
So your problem is obvious. Look at this
settings.edit().putString("uname", username);
Here you are trying to save String via SharedPreferences.Editor but if String could be permanently saved into SharedPreferences you need to call also commit() that means that you want to confirm your opperation(s). In other case, your value never be saved.
You can imagine this like one "transaction". If you won't commit transaction, any changes performed in it will be thrown away.
So, correct way is
settings.edit().putString("uname", username).commit();