BaseActivity/BaseFragment 添加 CoroutineScope

This commit is contained in:
luyao 2019-08-06 09:32:01 +08:00
parent 66cb112629
commit d6e73c8e72
3 changed files with 19 additions and 3 deletions

View File

@ -3,12 +3,15 @@ package luyao.util.ktx.base
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
/**
* Created by luyao
* on 2019/5/31 15:44
*/
abstract class BaseActivity : AppCompatActivity() {
abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -31,4 +34,9 @@ abstract class BaseActivity : AppCompatActivity() {
val intent = Intent(this, z).putExtra(name, value)
startActivity(intent)
}
override fun onDestroy() {
super.onDestroy()
cancel()
}
}

View File

@ -4,12 +4,15 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
/**
* Created by luyao
* on 2019/6/10 10:47
*/
abstract class BaseFragment : androidx.fragment.app.Fragment() {
abstract class BaseFragment : androidx.fragment.app.Fragment(), CoroutineScope by MainScope() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(getLayoutResId(), container, false)
@ -26,4 +29,9 @@ abstract class BaseFragment : androidx.fragment.app.Fragment() {
abstract fun initView()
abstract fun initData()
override fun onDestroy() {
super.onDestroy()
cancel()
}
}

View File

@ -21,7 +21,7 @@ open class BaseViewModel : ViewModel(), LifecycleObserver {
}
suspend fun <T> launchIO(block: suspend CoroutineScope.() -> T) {
suspend fun <T> launchOnIO(block: suspend CoroutineScope.() -> T) {
withContext(Dispatchers.IO) {
block
}