Vasko Vasilev
Vasko Vasilev

Reputation: 574

SharedPreferences does not pass get the saved value

I am working on android project built partially in kotlin and partially in java. I am trying to pass information from a kotlin fragment class to a java class. I have come to see that there is a problem with the passing of information as I do not receive the needed value. After some debugging I have seen that the information is successfully stored, however when the information from shared preferences is accessed, only the default value is returned.

This is the code in the kotlin class. When a button is clicked it changes the value of a boolean variable to the opposite one, sets the text of a button to true/false and saves the value of the variable in shared preferences.

 btnStyle.setOnClickListener() {
            styleHasChanged = !styleHasChanged;

            if(styleHasChanged  == true){
                btnStyle.setText("true")
            }else{
                btnStyle.setText("false")
            }

            val sharedPref : SharedPreferences?= activity?.getPreferences(MODE_PRIVATE);
            sharedPref?.edit()?.putBoolean("bla", styleHasChanged)?.apply()
}

This is the java class. Shared preferences is called inside a function which chooses a filepath based on the received value.

 public static String getHtmlContent(Context context, String htmlContent, Config config) {

    SharedPreferences sharedPreferences = context.getSharedPreferences("bla",MODE_PRIVATE);
    boolean hasStyleChanged = sharedPreferences.getBoolean("bla", false);

    //moj
    String cssPath;


        if (!hasStyleChanged) {
            cssPath = String.format(context.getString(R.string.css_tag), "file:///android_asset/css/Style.css");
        } else {
            cssPath = String.format(context.getString(R.string.css_tag), "file:///android_asset/css/Style2.css");
        }

This is where the problem arises. Shared preferences in the java class always fetches the default value, no matter if the button is clicked or not.

Upvotes: 2

Views: 828

Answers (2)

earthw0rmjim
earthw0rmjim

Reputation: 19417

The getPreferences method implicitly uses the class name of the Activity as the preference file name. By passing "bla" as the file name to getSharedPreferences you're trying to fetch the saved value from a different file.

If you would like to access the same preferences accross your application, then either use getSharedPreferences (for both writing and reading the preference) with the same file name, or use the getDefaultSharedPreferences static method of PreferenceManager to obtain the default SharedPreferences instance.

You should change your code to something like this:

val sharedPref : SharedPreferences? = activity?
        .getSharedPreferences("someFileName", MODE_PRIVATE)
sharedPref?.edit()?.putBoolean("bla", styleHasChanged)?.apply()

And the Java part:

SharedPreferences sharedPreferences = context
        .getSharedPreferences("someFileName", MODE_PRIVATE);
boolean hasStyleChanged = sharedPreferences.getBoolean("bla", false);

Upvotes: 2

apksherlock
apksherlock

Reputation: 8371

Your context.getSharedPreferences("PreferencesFileName",MODE_PRIVATE) should be the same even when using the activity?.getPreferences("PreferencesFileName",MODE_PRIVATE). In you code it's not like that.

Plus a simple suggestion, because I'm not sure if you're clear on that. The bla is the key for your boolean value not your Preferences file name. I mean it could be, but it's better to separate.

Upvotes: 1

Related Questions