If I would like to call api when Fragment onPause....
# android-architecture
s
If I would like to call api when Fragment onPause. Is it the case to use
GlobalScope.launch
?
Copy code
// Fragment

override fun onPause() {
    viewModel.leave()
}
Copy code
// ViewModel

fun leave() {
    GlobalScope.launch {
        repository.leave() 
    }
}
e
I would turn leave into a suspension function. And then you could say mainScope.launch and then withContext(Dispatchers.IO) but it would be done better using lifecycleowner
Inside of your suspension function you could do withContext
s
Does the api get cancel when it bound to lifecycleowner? I would like to call api when view destroying. But the coroutine will cancel all api when the view destroy.