0

I'm trying to make this function accessible from all files but if I define it outside class in Kotlin file, it shows me error for getSharedPreferences as unresolved reference. I think this is my answer but I don't know how to declare top-level function. How it can be done?

package com.example.app
    fun retrieveData() {
        val mypref = getSharedPreferences ("mypref", Context.MODE_PRIVATE)
        val radiotext=mypref.getString("data","")
    }
Pranciskus
  • 487
  • 1
  • 6
  • 17

2 Answers2

2

Declaring a top level function is as simple as not putting it within the braces { } of a class declaration.

A SharedPreferences can only be retrieved from an Android Context. Make your function an extension of Context:

fun Context.retrieveData() { //...

Then you can use it from within any Context type object, such as calling it from some function inside your Activity. If calling it from a Fragment (which is not itself a Context object), you'd have to call it on a context: context.retrieveData().

Making it an extension function is just a way of rearranging how you call it. You could also define your function to take a Context argument:

fun retrieveData(context: Context) {
    val mypref = context.getSharedPreferences("mypref", Context.MODE_PRIVATE)
    val radiotext=mypref.getString("data","")
}

In this case, you would always need to pass a Context. If you're in an Activity, you can pass this or pass context, which is a property of an Activity.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Thanks, it worked in my case, now I have problem with `var image_view = findViewById(R.id.TextView1)` `findViewById` = `Unresolved reference`. Maybe it's possible to solve this too or even get rid of `findViewById`? It's also in top-level. Whether is possible to use view in top-level? – Pranciskus Dec 03 '19 at 21:43
  • `findViewById` is a function of Activity. It can only find a view that is in the view hierarchy of the view you have set on an Activity. So it doesn't make sense to make a top-level function that would use it. You could do it by making your function take an Activity as an argument, but it would be a poor design choice because it would only work on the particular Activity that uses that view. – Tenfour04 Dec 03 '19 at 21:50
  • If you are using the Android KTX libraries (as new projects do by default), then you don't need `findViewById` at all. You can simply use the id name of your view as a property. – Tenfour04 Dec 03 '19 at 21:52
1

I see two options:

  1. Change functions to take in SharedPreferences as a parameter:
package com.example.app

    fun retrieveData(shPref: SharedPreferences): String {
        val radioText = shPref.getString("data", "")
        return radioText
    }
  1. Change functions to be an extension for SharedPreferences object take in SharedPreferences as a parameter:
package com.example.app

    fun SharedPreferences.retrieveData(): String {
        val radioText = this.getString("data", "")
        return radioText
    }

And you obviously need to get SharedPreference object beforehand to use this functions.