1

I'm just starting with Kotlin to make Android Apps (it's my first programming language) and I'm using a CountDownTimer. I want to access the p0 parameter globally, but I don't know how to. Here's the code:

class MainActivity : AppCompatActivity() { 

    var score: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

      var timeTimer = object : CountDownTimer(10000, 1000) {
          override fun onFinish() {
              timeView.text = "Time's up!"
          }

          override fun onTick(p0: Long) {
              timeView.text = "Time's left: " + p0/1000
          }
      }.start()
    }

    fun point (view: View) {
        score++
        p0 = p0 + 1000
        scoreView.text = "Score: $score"
    }
}

Function point is called by clicking a button. The thing I'm trying to achieve is making CountDownTimer longer by 1 second every time a user clicks a button.

This line p0 = p0 + 1000 obviously won't work because p0 is inside another code block. Is there any way to make it globally accessible? I've thought about putting CountDownTimer outside onCreate and just starting it in the onCreate, but I think it still wouldn't work as p0 is still inside CountDownTimer code block.

Droniu
  • 13
  • 2

2 Answers2

0

I don't know what's the reach of your App, but you can do a couple of things:

  • Create a custom Application class for your App and check the variable there. You need to create a class that extends from Application and add that class into your manifest, in the <application/> tag under the android:name attribute. You can access your custom Application class from anywhere by using it like a Singleton, for example MyApp.getInstance().setPvalue(someIntValue).

  • Maybe you want to save that value in your SharedPreferences so It's kept saved if you close the App.

  • Maybe you want to have that value in a Service, so if you close the App the timer is still going to be counting down.

I answer a pretty similar question the other day:

How to keep the countDownTimer counts after i swap the activity?

Hope it helps.

4gus71n
  • 3,717
  • 3
  • 39
  • 66
0
var timeTimer = object : CountDownTimer(10000, 1000) {
      override fun onFinish() {
          timeView.text = "Time's up!"
      }

      override fun onTick(p0: Long) {
          timeLeft = p0  //timeLeft is global --only way I think to keep track remaining time.
          timeView.text = "Time's left: " + p0/1000
      }
  }.start()

This should be global (better at the bottom after all methods of the Activity)

fun resetTimer(time){
   timeTimer.cancel()
   timeTimer = object : CountDownTimer(time, 1000) {
      override fun onFinish() {
          timeView.text = "Time's up!"
      }

      override fun onTick(p0: Long) {
          timeLeft = p0  
          timeView.text = "Time's left: " + p0/1000
      }
  }.start()

}

So finally

fun point (view: View) {
    score++
    resetTimer(timeLeft + 1000)
    scoreView.text = "Score: $score"
}
Ambareesh B
  • 499
  • 1
  • 5
  • 14