I have a big problem with the SharedPreferences in Android. The preferences are adding unwanted chars to one of my string values once the application is closed. Actually it is a configurable escape sequence.
I have a simple setup, a MainActivity containing
#Override
protected void onStart() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sequence = prefs.getString("escape_sequence", "");
}
And a preferences screen where the value is set. When i open the app go to the prefences screen, set the value correctly to \n\n and go back to the MainActivity the breakpoint is correctly displaying the sequence as Java.lang.String, value(char[2])= "\n\n", count=2. When i am now restarting the app through android studio the same breakpoint in the code suddenly displays: Java.lang.String, value(char[6])= "\n\n ", count=6, containing 4 Space and 10 escape \u0000 characters.
Can anybody why this is happening and what i can do about that?
BTW i'm not touching the SharedPreferences.Editor anywhere in the App so far. I is strictly done via the PreferencesScreen. So no overwrite is done anywhere in the app. The default values shouldn't be applied either, however the setting is android:defaultValue="\n\n" anyway.
EDIT:
I found the reason: android adds the spaces if a newline is at the end of the preference. I have no idea why.
EDIT:
Here is the custom preference code:
public class SequencePreference extends DialogPreference {
EditText sequenceInput;
public SequencePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_preference_sequence);
setPositiveButtonText(R.string.settings_sequence_ok);
setNegativeButtonText(R.string.settings_sequence_cancel);
setDialogIcon(null);
}
#Override
protected View onCreateDialogView() {
View view = super.onCreateDialogView();
sequenceInput= (EditText)view.findViewById(R.id.sequence_input);
return view;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
// When the user selects "OK", persist the new value
if (positiveResult) {
String sequenceValue = new String( sequenceInput.getText().toString() );
String[] parts = sequenceValue.split("-");
if(parts.length == 2) {
persistString(parts[1]);
}
}
}
}
I think this is a bug in Android API 18 and newer where extra whitespace is injected when a SharedPreferences string ends with \n. For more information, see:
https://code.google.com/p/android/issues/detail?id=159799#c6
https://code.google.com/p/android/issues/detail?id=159799#c7
after you retrieve the saved string, place a trim().
example
String sequence2 = sequence.trim()
Related
I just curious what makes a value inside of some variable become empty again or back to its initial value in the android life cycle.
First lets take a look at how i create a variable :
public class myData {
public static String myCode = "";
public static String getData(String Choice) {
String theData = "";
if ("Code".equals(Choice) {
theData = myCode;
}
return myCode;
}
public static void setData(String setData,String Choice) {
if ("Code".equals(Choice) {
myData.myCode = setData;
}
}
}
If I want to fill the variable, i usually do this :
myData.setData("value of variable","Code");
And if I want to get the value of the variable, I usually do this :
myData.getData("Code");
I just want to know what makes my variable gone inside of android lifecycle, of course excluding when the application is closed.
I have to try to Log and show the value in onstart , oncreate, onresume and onrestart. And all of them is still have the value inside of my variable intact without any problem.
My client always tells me that my application sometimes gets crash when they open some activity. I also ask if they did something while using my application,
some of them answer that the application get crashed after they got a phone call and when the phone call is ended, the application is started with a crash.
some of them also said that when they open the application and then idle the phone withouth closing the application until the phone become black screen, and when they open it again the application get crashed.
After I check the log, the problem was the variable become empty. which is why I want to know is there another possibilites that makes the value inside of the variable become empty?
As John Lord saying, on low-end device variables might back to its initial value again if there is not enough memory.
So for future reference, I use a shared preference to counter it, here is my structure for fetching the data :
public class myActivity extends AppCompatActivity {
String myCode = "";
protected void onCreate(Bundle savedInstanceState) {
....
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE);
myCode = sharedPreferences.getString("Code",null);
....
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE);
myCode = sharedPreferences.getString("Code",null);
}
}
And here is how i set the data :
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myData",Context.MODE_PRIVATE);
sharedPreferences.edit().putString("Code","Hello World").apply();
I hope it will be helpful for those who want to search the same thing
I am trying to store and retrieve values in shared preferences but first time when I open the app, the shared preference value is NULL. From next time, it shows the values of previous key.
This is my code,
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Set<String> set;
sharedPreferences = getApplicationContext().getSharedPreferences(mypreference, Context.MODE_PRIVATE);
databaseReferenceImages.child(postkey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
SharedPreferences.Editor editor = sharedPreferences.edit();
Set<String> sharedlist = new HashSet<String>();
sharedlist.addAll(second);
editor.putStringSet("notifications", sharedlist);
editor.apply();
for (String a : sharedlist) {
Log.d("thesharedlist", a);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
set = sharedPreferences.getStringSet("notifications", null);
if (set != null) {
for (String a : set) {
Log.d("thesetis", a);
}
}
Here I want the values for a particular "POSTKEY"(Poskey will be different for each post)
I want to get the value of array list or "SET" outside of onDataChange method so I used shared Preferences. Its working fine for a String but not for Array list or Set. The following problem occurs,
When I open the app for the first time and click a particular post,
the value of the shared preference value which is returned is NULL.
When I open the same post for the second time, the values are
returning perfectly.
When I open second post ,the values of
previous post are returning.
When I open the first post again, the values of second post are returning.
I don't know where I am wrong. I tried clearing the values of shared preferences each time the app opens but the same problem occurs. I even followed this link but nothing worked. Any help would be great. Thanks in advance.
If you don't want to get null first time, you can do one of these 2 things:
1- call getStringSet differently
sharedPreferences.getStringSet("notifications", "it isn't saved yet!");
2- save default value to sharedpreference before getStringSet
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("notifications", sharedlist);
editor.apply();
If you want to get current post not previous one, you should save it before. my guess is that you click on post number one and it's stored in sharedpreference and when you want to check the second one, it returns first one.
I have solved the issue by storing it in sharedpreference after 1500ms.
This is my code,
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
set = sharedPreferences.getStringSet("notifications", null);
if (set != null) {
for (String a : set) {
Log.d("thesetis", a);
}
}
}
}, 1500);
I have a PreferenceScreen in which the user is capable, if system can't autodetect it, to enter the device's phone number. I'm still learning this part of Android but I managed to understand a bit of PreferenceScreens by examples provided by Android SDK itself and a few tutorials.
What I want is that the user can save the phone number only if null or valid, where by "valid" I mean running a generic validation logic (ie. an anonymous method that returns true or false, that can be reused in any possible situation*) or better, just to simplify things, ^(\+39)?3[0-9]{9}$
For now I have the following XML snip
<EditTextPreference
android:inputType="phone"
android:key="#string/preference_phoneNo"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="#string/pref_phoneNumber_description"
android:title="#string/pref_phoneNumber" />
and following code by courtesy of Eclipse New Activity wizard:
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
return;
}
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.preference_phoneNo)));
}
addPreferenceFromResource is supposed to load the XML node and add the preference to the screen, while binPreferenceSummaryToValue is supposed to make description text change when preference is updated. Just for sake of completeness for those who don't like code courtesy of the IDE, the second method is provided by Eclipse who also provides a private class in the code file that is
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
In the general case, what should I do to perform validation logic before the preference gets saved when I click OK on the preference editor? Where is the validation logic to be put in a PreferenceScreen?
*Aren't we all here to learn?
Android has a built in helper method for this.
String phoneNumber = ...;
boolean valid = PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber);
For a generic, re-usable method, here's the implementation of that method in PhoneNumberUtils, courtesy of AOSP (Apache licensed)
private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
Pattern.compile("[\\+]?[0-9.-]+");
...
public static boolean isGlobalPhoneNumber(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber)) {
return false;
}
Matcher match = GLOBAL_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
return match.matches();
}
Validation should occur within a Preference.OnPreferenceChangeListener , in the onPreferenceChange method. Simply return false if you don't want the value to be saved.
Example snippet:
private static Preference.OnPreferenceChangeListener myListener =
new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof PhoneNumberPreference) {
return isGlobalPhoneNumber(value);
}
}
...
As a note, since you're starting off with the Settings activity generated by the Eclipse wizard, this listener has already been built for you. You just need to edit it to include validation of the phone number (assuming that's what's being edited), and to return false if the number is invalid, so it won't be saved to preferences.
Is there a way to create a preference in a PreferenceFragment that accepts only integer values? I could implement an EditTextPreference and register an OnPreferenceChangeListener in which I could reject the change if the user enters a a string that is not a number, but I would prefer something that is meant for holding only numbers and that does not allow users to enter anything else, maybe showing only a dial pad keyboard.. I don't such a preference exist, since every descendant of Preference is mapped onto a Boolean (CheckBoxPreference), a String (*EditTextPreference) or a String array (MultiSelectListPreference), i.e. there are no preferences mapped onto integers, but maybe some of you can give me an hint or at least tell me if there are better solutions than the one I've proposed above.
Solution proposed by Grey:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new NumberKeyListener() {
#Override
public int getInputType() {
// The following shows the standard keyboard but switches to the view
// with numbers on available on the top line of chars
return InputType.TYPE_CLASS_NUMBER;
// Return the following to show a dialpad as the one shown when entering phone
// numbers.
// return InputType.TYPE_CLASS_PHONE
}
#Override
protected char[] getAcceptedChars() {
return new String("1234567890").toCharArray();
}
});
Shorter solution, which does not allow varying the keyboard to dialpad but requires less code:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new DigitsKeyListener());
I don't like just one thing about this solution: the user can enter 0000 and it is accepted and saved in the shared preference (which is a String) as "0000".. this requires you to implement a OnSharedPreferenceChangeListener to intercept changes to shared preferences and remove leading zeros in this preference or implement a change listener directly on this preference and return false to refuse numbers with trailing zeros (tell me if there is a better solution to this last problem which does not involve implementing your own Preference). It would be beautiful if we could modify the newValue in OnPreferenceChangeListener..
There is an easier way to do this. Just set in the EditTextPreference in your XML to android:numeric="integer" (you can set it to integer, signed or decimal).
Eclipse (or whatever tool you are working in) won't show you this as a possible attribute, but just write it in your EditTextPreference and clean your project. You will see that it won't give you any errors. :)
After reading the proposed solutions, I still think a custom preference is the way to go. I did read your remark in bold text, but setting up a basic EditTextIntegerPreference is actually super simple and it will solve the remaining issue you have when the user enters for example "0000".
Just a note up front: since you normally want to be able to use preferences in several places in your app, as far as I'm concerned, a proper implementation of an EditTextIntegerPreference would store its value against an Integer. That way you'll be able to retrieve the int value anywhere, without the need to first parse or cast it.
However, to keep this answer to the point and compact, I'm actually going to show an extension of a regular EditTextPreference, which means that under the hood, the value will still be stored as a string. If you're really keen on getting the 'proper' implementation to work, I don't mind writing that up later on. It shouldn't be too tricky though, so you might want to have a crack at it yourself first. :)
public class EditTextIntegerPreference extends EditTextPreference {
private Integer mInteger;
public EditTextIntegerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context) {
super(context);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
#Override public void setText(String text) {
final boolean wasBlocking = shouldDisableDependents();
mInteger = parseInteger(text);
persistString(mInteger != null ? mInteger.toString() : null);
final boolean isBlocking = shouldDisableDependents();
if (isBlocking != wasBlocking) notifyDependencyChange(isBlocking);
}
#Override public String getText() {
return mInteger != null ? mInteger.toString() : null;
}
private static Integer parseInteger(String text) {
try { return Integer.parseInt(text); }
catch (NumberFormatException e) { return null; }
}
}
The reason why this solves the "0000" issue, is that we're simply casting the user typed value to an int before we store it, and plug it back into a string upon retrieval. That means any leading zeroes (except for '0' as a number of course) will automagically disappear.
The 'number' input type will restrict the user to input only enter numbers, so the parseInteger(...) method is mainly present for sanity reasons, although it will catch a NumberFormatException if you try to enter an empty or null string. Again, you can make this more pretty, which I haven't done for now.
i guess u can call getEditText() to get a EditText obj , then call setKeyListener with a NumberKeyListener.
You should use PreferenceActivity which is basically used to show preferences like an xml file. In this activity you can use checkboxes, edit text and save values to preferences.
You can also reopen the dialog when you get an exception after trying to parseInt() the number.
Here is a sample code:
public class MyEditTextPreference extends EditTextPreference {
private EditText _editText;
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
_editText = getEditText();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
try {
Integer.parseInt(_editText.getText().toString());
} catch (NumberFormatException e) {
((Dialog) dialog).show();
}
}
}
What I want to do is I am working on a game of life program. I want to take the time delay and make it a preference, but I want to make it available for people to type in a specific time. The number can be in miliseconds or seconds.
However I'm a little stuck on how to proceed, I haven't been able to find a simple preference that already handles this, but there might be one. Is there an easy way to make this preference and confirm that the entered data is an integer or afloat?
Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters.
EditTextPreference pref = (EditTextPreference)findPreference("preference_name");
pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
You can also enforce it with the xml attribute android:numeric. The possible relevant values for this attribute are decimal and integer.
You can also do this directly in your preferences.xml. Something like this would work:
<EditTextPreference
android:defaultValue="100"
android:dialogTitle="#string/pref_query_limit"
android:inputType="number"
android:key="pref_query_limit"
android:summary="#string/pref_query_limit_summ"
android:title="#string/pref_query_limit" />
If you are using a PreferenceActivity which you probably are, there is not one available.
You will need to do something like this:
/**
* Checks that a preference is a valid numerical value
*/
Preference.OnPreferenceChangeListener numberCheckListener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
//Check that the string is an integer.
return numberCheck(newValue);
}
};
private boolean numberCheck(Object newValue) {
if( !newValue.toString().equals("") && newValue.toString().matches("\\d*") ) {
return true;
}
else {
Toast.makeText(ActivityUserPreferences.this, newValue+" "+getResources().getString(R.string.is_an_invalid_number), Toast.LENGTH_SHORT).show();
return false;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get XML preferences
addPreferencesFromResource(R.xml.user_preferences);
//get a handle on preferences that require validation
delayPreference = getPreferenceScreen().findPreference("pref_delay");
//Validate numbers only
delayPreference.setOnPreferenceChangeListener(numberCheckListener);
}
In Android Jetpack Preference things changed, to access EditText you have to access like this
val preference = findPreference<EditTextPreference>(getString(R.string.pref_numdefault_key))
preference?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
}