現在Kotlinの勉強をしているものです。
非同期処理でスタートボタンを押したらテキストビューが1秒ずつ動き出す。ストップボタンを押したらテキストビューに『停止』と表示される処理を作りました。
ここでパターン1として下記コードで無事に実現することができました。(処理したいコードをwithContext(Dispatchers.Main) {}の中に書いたもの)
kotlinパターン1
1package com.example.kotlin_coroutines 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.widget.Button 6import android.widget.TextView 7import androidx.lifecycle.lifecycleScope 8import kotlinx.coroutines.* 9 10class MainActivity : AppCompatActivity() { 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 15 val startButton = findViewById<Button>(R.id.startButton) 16// ボタンを連打したときに何度も処理してしまうのを防ぐため。jobを設定する 17 var job: Job? = null 18 startButton.setOnClickListener{ 19// 下記にすることで連打したときに何度も処理されることを防げる。coroutinesが複数起動しなくなるということ。 20 if (job?.isActive == true) return@setOnClickListener 21 job = lifecycleScope.launch { 22 23 24// result内のスコープがIOスレッドで実行される 25 withContext(Dispatchers.IO) { 26 27 28 29 30 31// Main内のスレッドがIOスレッド内でメインスレッドも実行可能となる。 32 withContext(Dispatchers.Main) { 33 34 val textView = findViewById<TextView>(R.id.textView) 35 var count = 0 36 try { 37 while (true) { 38 textView.text = "${count}秒" 39 count++ 40// 遅延実行(1000で1秒) 41 delay(1000) 42 } 43// Jobがtrueじゃない(非アクティブ)場合の命令 44 } catch (e:Exception) { 45 println(e) 46 } finally { 47 textView.text = "停止" 48 } 49 } 50 51 } 52 53 54 } 55 } 56 val stopButton = findViewById<Button>(R.id.stopButton) 57 stopButton.setOnClickListener { 58// jobを非アクティブにする 59 job?.cancel() 60 } 61 } 62}
そしてこれはきっと例外になるだろうと検証にパターン2を作成しました。パターン1の処理したい内容をwithContext(Dispatchers.IO) {}の中にコピペしたもの。がパターン2です。
Kotlinパターン2
1package com.example.kotlin_coroutines 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.widget.Button 6import android.widget.TextView 7import androidx.lifecycle.lifecycleScope 8import kotlinx.coroutines.* 9 10class MainActivity : AppCompatActivity() { 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 15 val startButton = findViewById<Button>(R.id.startButton) 16// ボタンを連打したときに何度も処理してしまうのを防ぐため。jobを設定する 17 var job: Job? = null 18 startButton.setOnClickListener{ 19// 下記にすることで連打したときに何度も処理されることを防げる。coroutinesが複数起動しなくなるということ。 20 if (job?.isActive == true) return@setOnClickListener 21 job = lifecycleScope.launch { 22 23 24// result内のスコープがIOスレッドで実行される 25 withContext(Dispatchers.IO) { 26 27 28 val textView = findViewById<TextView>(R.id.textView) 29 var count = 0 30 try { 31 while (true) { 32 textView.text = "${count}秒" 33 count++ 34// 遅延実行(1000で1秒) 35 delay(1000) 36 } 37// Jobがtrueじゃない(非アクティブ)場合の命令 38 } catch (e:Exception) { 39 println(e) 40 } finally { 41 textView.text = "停止" 42 } 43 44 45// Main内のスレッドがIOスレッド内でメインスレッドも実行可能となる。 46 withContext(Dispatchers.Main) { 47 48 49 } 50 51 } 52 53 54 } 55 } 56 val stopButton = findViewById<Button>(R.id.stopButton) 57 stopButton.setOnClickListener { 58// jobを非アクティブにする 59 job?.cancel() 60 } 61 } 62}
このパターン2の実行結果がスタートボタンを1回押すと『停止』と表示され(ここまでは想定内)
スタートボタンを2回押すとなぜかカウントが正常に始まってしまいました。
この現象はなぜ起こるのでしょうか?自分には理解できなかったのでどなたか解説お願いしたいです。
宜しくお願いします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。