Paul Woitaschek
03/26/2019, 8:36 AMribesg
03/26/2019, 9:19 AMCoroutineScope by MainScope()
and call cancel()
in onDestroy()
Paul Woitaschek
03/26/2019, 9:22 AMribesg
03/26/2019, 9:24 AMlaunch { }
or something like that in onStart
, then it’ll die with the rest if I understood it correctlyPaul Woitaschek
03/26/2019, 9:26 AMribesg
03/26/2019, 9:27 AMPaul Woitaschek
03/26/2019, 9:27 AMribesg
03/26/2019, 9:28 AMonStop
and onDestroy
... You just have to be sure that there’s nothing left after onDestroy
. Are you sure the gains are worth the complexity of what you want to do?Paul Woitaschek
03/26/2019, 9:32 AMribesg
03/26/2019, 9:34 AMview?.something
so that it’s automagicPaul Woitaschek
03/26/2019, 9:35 AMribesg
03/26/2019, 9:35 AMActivity
and Fragment
and then use thatPaul Woitaschek
03/26/2019, 9:36 AMribesg
03/26/2019, 9:37 AMPaul Woitaschek
03/26/2019, 9:47 AMribesg
03/26/2019, 10:06 AMChildren of a supervisor job can fail independently of each other.
streetsofboston
03/26/2019, 12:27 PMI.e. I clear my views in onDestroyView, so I don't want a coroutine I started in onCreateView access null views after onDestroyView@Paul Woitaschek A Fragment is a Lifecycle owner and its lifecycle is per default tied to the Fragment itself. This my cause problems as you described above. Instead of this:
viewModel.liveData.observe(this, Observer { ... } )
}
Do this
viewModel.liveData.observe(viewLifecycleOwner, Observer { ... } )
}
where this
is replaced by this.viewLifecycleOwner
This makes sure the LiveData observer will not get callbacks on dead Fragments and only when Views have been created properly, ie between onViewCreated and onViewDestroyed.Paul Woitaschek
03/26/2019, 3:35 PMstreetsofboston
03/26/2019, 3:39 PMviewLifecycleOwner
to your own life-cycle observers, which could guard against encountering null values for your views in your Fragments.Paul Woitaschek
03/26/2019, 3:58 PMstreetsofboston
03/26/2019, 4:01 PMDico
03/27/2019, 3:48 PMActivity
sadlystreetsofboston
03/27/2019, 5:03 PMDico
03/27/2019, 5:03 PMJemshit Iskenderov
04/11/2019, 1:14 PMcoroutineScope
properties (not implement them) that gets cancelled on different lifecycle event, then every .launch
will use correct version of coroutineScope
. But don’t rely on job.cancel()
, it will cancel parent job and children, but your “computation” code will still continue to run (null view access might be done after cancelled). So parentJob.isActive
check should be done. @Paul Woitaschekstreetsofboston
04/11/2019, 1:20 PMLiveData
in your ViewModel, your Activity's or Fragment's observers should never get notified when View references are null.
It's true, Jobs are cancelled only at suspension points, not just anywhere in the code. Either rely on those points or query the isActive
property and bail out.