https://kotlinlang.org logo
#android
Title
# android
t

touge

06/26/2019, 8:34 AM
I have confuse when using lifecycleScope in fragment, why the fragment’s context is null inside lifecycleScope? I also asked this on stackoverflow, here is the link: https://stackoverflow.com/questions/56415530/why-the-fragments-context-null-inside-lifecyclescope
g

gildor

06/26/2019, 8:37 AM
It’s make sense.
lifecycleScope
has lifecycle of fragment and it’s != to lifecycle of context or view
Answered on SO
t

touge

06/26/2019, 8:54 AM
Thanks, really helped!
g

gildor

06/26/2019, 9:14 AM
Thanks, really help but seems I can’t upvote this with enough scorev
You can acceprt answer instead of upvote 😁
😁 1
t

touge

06/26/2019, 9:26 AM
First time ask question on stackoverflow and founded the accept button😂
And I also have a doubt in your answer, the lifecycleScope of Fragment starts from onCreate until onDestroy, are as I know these lifecycle’ events inside onAttach to onDetach? If in, when using lifecycleScope of Fragment it self may cause a update UI exceptions but not a IllegalStateException of empty context.
g

gildor

06/26/2019, 9:35 AM
may cause a update UI exceptions but not a IllegalStateException of empty context
No, because this job may executing even when context is null
and context is null when fragment is detached
t

touge

06/26/2019, 9:41 AM
I mean that because the onDestroy event happen before onDetach, so the code inside lifecycleScope will be canceled before onDetach
g

gildor

06/26/2019, 9:42 AM
So yes, onCreate initially called after onAttach initially, but context is not avavilable after onDetach and before next onAttach
I see what you mean
t

touge

06/26/2019, 9:43 AM
So why there is a IllegalStateException of empty context when I use the Fragment’s lifecycleScope…
g

gildor

06/26/2019, 9:46 AM
huh
okay, so essentially fragment have context only after super.onCreateView, not directly related to onAttach/onDetach, I updated answer
e

Eric Martori

06/26/2019, 9:53 AM
with the new android support for coroutines in fragment you have
viewLifecycleOwner.lifecycleScope
which should follow the lifecycle of the view instead o the fragment and prevent this issues
g

gildor

06/26/2019, 9:53 AM
yes, I already mentioned this on SO
I’m digging to source code of Fragment and Fragment manager and understand that I understand less and less 🙈
🤣 1
t

touge

06/26/2019, 10:45 AM
I try to use
viewLifecycleOwner.lifecycleScope
, but it throw a Exception before onCreateView() or after onDestroyView(), for protected the method be called in a safe way, how about the below way?
Copy code
fun foo() = viewLifecycleOwnerLiveData.value?.lifecycleScope?.launch {
}
g

gildor

06/26/2019, 1:23 PM
Sure, it throws exception, and this is the whole point, do not start it out of view lifecycle
e

Eric Martori

06/26/2019, 3:16 PM
you can use:
viewLifecycleOwner.lifecycleScope.launchWhenCreated {  }
that way inside the coroutine you know that view state is atleast created
👍 1
59 Views