protected val coroutineMarkerSup = SupervisorJob()
protected val myMarkerSQLScope = CoroutineScope(Dispatchers.Main + coroutineMarkerSup)
fun doSomething() {
myMarkerSQLScope.launch {
val user = withContext(Dispatchers.IO){
doSomehingInTheBackGround()//gets the SQL database
}
//Any code here is blocked till doSomethingInTheBackGround is finished
finalTaskOnceCoroutineFinished()
}
}
fun finalTaskOnceCoroutineFinished() {
//recyclerView.adapter = adapter
adapter.notifyDataSetChanged()//refresh recyclerview
}
@CallSuper
override fun onPause() {
super.onPause()
coroutineMarkerSup.cancel()
}
然后,一旦在子活动中进行了会影响父活动的更改,我将在父活动中重新启动协程,如下所示。
override fun onRestart() {
super.onRestart()
coroutineMarkerSup.start()
doSomething()
}