Unresolved reference: getPreferences
Asked Answered
N

4

5

I am trying to store a boolean value which is changed every time a button is clicked. I want to do this using shared preferences, however I keep running into this error: Unresolved reference: getPreferences

This is my code:

btnStyle.setOnClickListener() {
            styleHasChanged = !styleHasChanged;

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

          //  AppUtil.saveConfig(activity, config)
          //  EventBus.getDefault().post(ReloadDataEvent())

          var sharedPref : SharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
            var editor = sharedPref.edit()
            editor.putBoolean("bla", styleHasChanged)
            editor.commit()



        }
Nannienanning answered 19/6, 2019 at 8:2 Comment(2)
Show your stacktrace error.Tartan
It will not compile. On the following line: var sharedPref : SharedPreferences = this.getPreferences(Context.MODE_PRIVATE); the letters getPreferences are red and when I hover over them I receive this: Unresolved reference: getPreferencesNannienanning
G
6

Is this a Fragment or an Activity? This seems code written in fragment or somewhere else. Because getPreferences() is method of activity and you need to have Activity's instance to call it .

Just have a Activity instance and call it as below . example for Fragment:-

btnStyle.setOnClickListener() {
        styleHasChanged = !styleHasChanged;
        if(styleHasChanged  == true){
            btnStyle.setText("true")
        }else{
            btnStyle.setText("false")
        }
        val sharedPref : SharedPreferences?= activity?.getPreferences(Context.MODE_PRIVATE);
        sharedPref?.edit()?.putBoolean("bla", styleHasChanged)?.apply()
    }
Greenleaf answered 19/6, 2019 at 8:13 Comment(1)
Yup, this works. The code is written in a fragment and this works perfectly.Nannienanning
S
14

For KOTLIN

If Activity then use this@ActivityName

var sharedPref : SharedPreferences = [email protected](Context.MODE_PRIVATE);

If Fragment then use activity!!

var sharedPref : SharedPreferences = activity!!.getPreferences(Context.MODE_PRIVATE);
Spunk answered 19/6, 2019 at 8:15 Comment(0)
G
6

Is this a Fragment or an Activity? This seems code written in fragment or somewhere else. Because getPreferences() is method of activity and you need to have Activity's instance to call it .

Just have a Activity instance and call it as below . example for Fragment:-

btnStyle.setOnClickListener() {
        styleHasChanged = !styleHasChanged;
        if(styleHasChanged  == true){
            btnStyle.setText("true")
        }else{
            btnStyle.setText("false")
        }
        val sharedPref : SharedPreferences?= activity?.getPreferences(Context.MODE_PRIVATE);
        sharedPref?.edit()?.putBoolean("bla", styleHasChanged)?.apply()
    }
Greenleaf answered 19/6, 2019 at 8:13 Comment(1)
Yup, this works. The code is written in a fragment and this works perfectly.Nannienanning
T
1

Try to open sharedPreferences via application context, like this:

application.getSharedPreferences("Your preference name", Context.MODE_PRIVATE)

All you need is context for opening preferences.

Tartan answered 19/6, 2019 at 8:8 Comment(3)
In the line you suggested application becomes the unresolved reference.Nannienanning
Show screenshot of your error or attach more code of your case.Tartan
This work in my case since I am using non Activity/Fragment class to get the preferences, thank you Sir!Tsunami
C
0

my solution for kotlin is:

//declarate in your class
 private lateinit var sharedpreferences: SharedPreferences
const val TOKEN_KEY = "token"



//declarate inside onCreate of activity
 sharedpreferences = requireContext().getSharedPreferences(SHARED_PREFS,Context.MODE_PRIVATE);

//get token_activo storage in android       
 token_activo = sharedpreferences.getString(TOKEN_KEY, null)

//save in android
 val editor = sharedpreferences.edit()
  editor.putString(TOKEN_KEY, "value_to_save")
  editor.apply()
Cesium answered 25/4, 2024 at 14:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.