Load variable value into a function in another function

0

I'm doing a simple game to learn how to program for Android with Kotlin. I am afraid of an activity (game) of the game and when you find a number it takes you to another atcivity (Finish), passing two information, that are in OnCreate.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val iFinish = getIntent()
    val numbers: String = iFinish.getStringExtra("LEVEL_NUMBER")
    val plays: String = iFinish.getStringExtra("NUMBER_PLAYS")

    textViewNumPlays.text = plays
    textViewRandom.text = numbers
}

Then I have two buttons, one that returns for the Main Menu, another restarts the game with the same previous configurations, but the one that restarts needs the val numbers and the function is out of the onCreate.

fun onButtonRestartClicked (view: View){
    val message1 = numbers.toString()
    val iRestart = Intent(this, GameActivity::class.java).apply {
        putExtra("EXTRA_MESSAGE", message1);
    }
    if (iRestart.resolveActivity(packageManager) != null) {
        startActivity(iRestart)
    }
}

My question is how to load this val numbers into a function outside the onCreate?

    
asked by Tiago David Furtado 13.01.2018 в 18:18
source

1 answer

0

I discovered my mistake Added a global variable var numbers = 0, then update it in onCreate.

var numeros = 0

fun onButtonMenuClicked (view: View){
    val iMenu = Intent(this, StartActivity::class.java)

    if (iMenu.resolveActivity(packageManager) != null) {
        startActivity(iMenu)
    }
    this.finish()
}

fun onButtonRestartClicked (view: View){
    val message1 = numeros.toString()
    val iRestart = Intent(this, GameActivity::class.java).apply {
        putExtra("EXTRA_MESSAGE", message1);
    }
    if (iRestart.resolveActivity(packageManager) != null) {
        startActivity(iRestart)
    }
    this.finish()
}

val iFinish = getIntent()
val numbers: String = iFinish.getStringExtra("LEVEL_NUMBER")
val plays: String = iFinish.getStringExtra("NUMBER_PLAYS")

textViewNumPlays.text = plays
textViewRandom.text = numbers
numeros = numbers.toInt()
    
answered by 13.01.2018 в 19:35