I want to set an EditTextPreference every launch of the activity.
For that i writed some code to set it but nothing happens:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.vidange);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor1 = settings.edit();
editor1.putString("prochain_vidange","12560");
editor1.commit();
}
What's the problem of this code ?
Thank you for your help.
Related
I have this code:
public class Register extends Activity {
private LinearLayout layout;
private TextView debug;
public static final String USER_CONFIG = "UserConfigs";
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
SharedPreferences settings = getSharedPreferences(USER_CONFIG, MODE_PRIVATE);
boolean registered = settings.getBoolean("registered", false);
layout = (LinearLayout) findViewById(R.id.layoutRegister);
if (!registered) {
debug = new TextView(this);
debug.setText("You have to register");
layout.addView (debug);
//TO DO user registration
settings.edit().putBoolean("registered", true);
settings.edit().commit();
} else {
debug = new TextView(this);
debug.setText("You have already registered");
layout.addView (debug);
//TO DO skip to next screen
}
}
}
But I'm always getting registered as "false" when I restart my app. I have tried to commit it on the onStop() as well and got the same result. I have seen other topics with this problem here but none of them had the same problem as I do.
Any ideas?
You can't do this:
settings.edit().putBoolean("registered", true);
settings.edit().commit();
You need to get the editor object, then make the changes:
Editor editor = settings.edit();
editor.putBoolean(...);
editor.commit();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(LoginActivity.this);
Editor edit = prefs.edit();
edit.putBoolean("registered", true);
edit.commit();
use this
The other answers are also correct.
You can also use this
settings.edit().putBoolean("registered", true).commit();
I am writing an app to save password with a login interface. The user can change the login password. First time, I use the following code to save the password, so that the password will not reset when the app relaunch:
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putString("pwd", currentPwd);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState){
currentPwd = savedInstanceState.getString("pwd");
}
But after I asked that, someone in this website suggested me to use "SharedPreferences". So, I changed the code as follow:
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
SharedPreferences settings = getSharedPreferences("setting", 0);
currentPwd = settings.getString("pwd", "abc");
}
#Override public void onStop(){
super.onStop();
SharedPreferences settings = getSharedPreferences("setting", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("pwd", currentPwd);
editor.commit();
}
After my experiment, I found that the password will be reset after one hour which the same as the first code. Have I changed it wrong for the second code? Or there are any suggested way to solve it? Thank you.
Not sure I'm following your code, but here is how I would do this:
To get password:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.getString("pwd", "abc");
To set password:
SharedPreferences.Editor editor = sp.edit();
editor.putString("pwd", currentPwd);
editor.commit();
Hope that helps
You can save value using bellow function.It contains only context, key and the value:
public void savePreferencesForReasonCode(Context context,
String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
To get more see this Answer
I have created an activity where i have used shared preferences for storing data..now in another activity i have an reset button..when i click on the reset button the data store will be lost..so how that can be done..my code is
code in activity1:
public void writeToRegister()
{
// Write history data to register
SharedPreferences preferences1 = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor1 = preferences1.edit();
editor1.putInt("iHistcount", CycleManager.getSingletonObject().iHistCount);
for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
{
editor1.putLong("dtHistoryDate"+Integer.toString(i), CycleManager.getSingletonObject().dtHistory[i].getTime());
}
editor1.commit();
}
public void readFromRegister()
{
// Read history data from register
SharedPreferences preferences1 = getPreferences(MODE_PRIVATE);
CycleManager.getSingletonObject().iHistCount=preferences1.getInt("iHistcount", 0);
for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
{
Long x=preferences1.getLong("dtHistoryDate"+Integer.toString(i), 0L);
CycleManager.getSingletonObject().dtHistory[i]=new Date(x);
}
}
code for Activity 2:
Button pBtnReset = new Button(this);
pBtnNextMonth.setOnClickListener(pBtnReset OnClickListener);
Button.OnClickListener pBtnReset OnClickListenernew Button.OnClickListener()
{
public void onClick(View arg0)
{
}
};
so what i have to write in second activity reset button so that it clear the stored data
Get your Editor and call clear() something like this:
Edit: as the user DDoSAttack mentioned.
There are two ways of getting SharedPreferences
1: getting default SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
2: getting specific SharedPreferences
SharedPreferences prefs = Context.getSharedPreferences("FileName", Context.MODE_PRIVATE);
and here is how you'll clear it.
public void clear()
{
SharedPreferences prefs; // here you get your prefrences by either of two methods
Editor editor = prefs.edit();
editor.clear();
editor.commit();
}
its very easy..
yourEditor.remove(" thing you want to remove on start");
and then give must
yourEditor.commit();
If you want to wipe all the data in a preference file call clear() from the SharedPreferences.Editor instance
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()
Use SharedPreferences.Editor clear() method.
See Documentation
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
I'm slowly working through an Android learning book and was given the following code to assign user data:
package com.androidbook.triviaquiz;
import android.app.Activity;
import android.content.SharedPreferences;
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefeditor.putString("UserName", "John Doe"); //**syntax error on tokens**
prefEditor.putInt("UserAge", 22); //**syntax error on tokens**
prefEditor.commit();
}
However, I get an error (lines indicated with comments) that underlines the period and says "misplaced construct" and also that underlines the arguments saying "delete these tokens". I have seen this done in other applications in the same format, I don't understand what is wrong.
Edit: Of course! Those statements cannot be put directly into the class at that level and must be inside a method, something like this:
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "John Doe");
prefEditor.putInt("UserAge", 22);
prefEditor.putString("Gender", "Male");
prefEditor.commit();
}
}
I think you may missed up OnCreate() method ,let be sure you should place the shared preference in your OnCreate() method... i just edited your code go through it
please go through the code...below
public class A extends Activity {
static SharedPreferences settings;
public static final String PREFS_NAME = "YourPrefName";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
Log.v("UserName"," - "+settings.getString("username","android"));
SharedPreferences.Editor editor = settings.edit();
editor.putString("username","Change Android");
editor.commit();
Log.v("UserName after changed editing preference key value"," - "+settings.getString("username","android"));
}
}
SharedPreferences will work out side a onCreate() method as long as it has a context:
SharedPreferences settings = getAplicationContext().getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
How can i create a function that will be run only once, on first installation? I am trying to create an information that displays one time.
thanks in advance.
You could use a flag in the shared preferences.
Then on every startup you check this flag. If it is not set you display your first time message and then set the flag.
For example:
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean firstStart = settings.getBoolean("firstStart", true);
if(firstStart) {
//display your Message here
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstStart", false);
editor.commit();
}
}