Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android preferences adding unwanted chars

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]);
            }
        }
    }
}
like image 603
patman Avatar asked Dec 26 '15 13:12

patman


2 Answers

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

like image 192
CompareAndSwap Avatar answered Oct 25 '22 09:10

CompareAndSwap


after you retrieve the saved string, place a trim(). example

String sequence2 = sequence.trim()
like image 30
Jose Tovar Avatar answered Oct 25 '22 09:10

Jose Tovar