I've written a kotlin class with an overridden fun and a fun to update a var into the class scope (I'm tragically new to Kotlin!)
class mySampleClass: sampleReference(){
var varToBeUpdated:String = "my string" //var in class scope to be updated
fun updateMyVar(gotString:String){
//I tried this, it didn't work
this.varToBeUpdated = gotString
// also this didn't work
varToBeUpdated = gotString
}
override fun sample(context: Context, intent: Intent){
//here i need my varToBeUpdated with new string
runSomeThing(varToBeUpdated)
//some work to be done here
}
}
In the place where I call the methods i do:
myObject.updateMyVar("new string")
myObject.sample()
I wonder how I can update the var i need, since I cannot add new args in the "fun sample", due to the fact it's overriding a class method
Thanks in advance, best regards to everyone :)
UPDATE: add my actual code, due to the fact that the class it seems unable to keep on the right updated value as i call the overrid method:
Here is my BroadcastReceiver, to check whem download is completed and them perform some action
class DownloadBroadcastManager: BroadcastReceiver() {
var myClassFilename:String = "default"
var myClassExtension:String = ".default"
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
//Show a notification
// here there's a log to check if var are updated
println("myTag - variables $myClassFilename, $myClassExtension")
Toast.makeText(context, "Download of $myClassFilename$myClassExtension completed", Toast.LENGTH_LONG).show()
// richiama azioni come player o display image o altro?
//player
var uri = Uri.parse (Environment.getExternalStorageDirectory().getPath() + "/Download/$myClassFilename$myClassExtension") //myClassExtension is ".mp3", dot is included, however it seems class is re-intialized as i call the method
println("myTag - uri: $uri")
println("myTag - context: $context")
var mPlayer = MediaPlayer() // I added this declaration (that's be re-done later) cause I had a problem in making the player running (of course giving it a valid path to a valid file). Now this is "junk code"
mPlayer.stop()
mPlayer.reset()
mPlayer.release()
mPlayer = MediaPlayer.create(context, uri) // here there's the proper declaration + initialization
mPlayer.start()
}
}
}
Here is the part from my DownloaderClass...
var brReceiver = DownloadBroadcastManager()
// shows when download is completed
println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: originals") //here shows the default: it's right
val intent = Intent(context, MainActivity::class.java)
brReceiver.myClassFilename = myTitle // inject filename
brReceiver.myClassExtension = ".mp3" // inject file extension
println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: modified") // here it shows my class property as correctly updated
brReceiver.onReceive(context, intent) // here, as calling the override fun, it get back to default value of the property
You can just do the following:
Get rid of updateMyVar
function:
class MySampleClass: SampleReference(){
var varToBeUpdated:String = "my string" //var in class scope to be updated
override fun sample(context: Context, intent: Intent){
//here i need my varToBeUpdated with new string
runSomeThing(varToBeUpdated)
//some work to be done here
}
}
Update varToBeUpdated
property directly:
val myObject = MySampleClass()
myObject.varToBeUpdated = "new string"
myObject.sample()
Update:
If you call brReceiver.onReceive(...)
the values in DownloadBroadcastManager
are updated. But you shouldn't do that. The Android framework calls it for you. And when it happens new instance of DownloadBroadcastManager
class is created and default values are set. We use Intents to pass data to BroadcastReceiver
, e.g. call intent.putExtra("filename", "yourFileName")
when you create BroadcastReceiver
, and call intent.getStringExtra("filename")
in onReceive()
function to get the value. Here is how to pass/get data to/from BroadcastReceiver