Is `await`ing in fragment A for a computation done...
# android
m
Is `await`ing in fragment A for a computation done in another fragment B possible in anyway? I know this is a dumb question, but I'm finding it hard to chain a sequence of asynchronous operations.
c
Iโ€™m not entirely sure what youโ€™re trying to do with
captureLocation
and
capturePhoto
, but doing it within the โ€œscopeโ€ of the fragment is generally considered bad practice. But using ViewModels as a shared layer between those fragments is perfectly fine. The ViewModel can make those async calls and dispatch the results to the fragments as-needed
b
Are you trying to emulate an activity result via a fragment?
i
This isn't a good idea, particularly when you take into account config changes and process death/recreation while on some other fragment
You absolutely can wrap the Fragment Result API up in a channel/Flow, but you'll need to be collecting that as part of a lifecycle method vs just as a suspending function https://developer.android.com/training/basics/fragments/pass-data-between
๐Ÿ‘† 1
m
@Casey Brooks I'm requesting location via fusedlocationproivider in
captureLocation()
and going to a new fragment that handles face capture in
capturePhoto()
. I need to somehow wait for these two things to happen and proceed accordingly.
@Ian Lake So I can do this properly only via a callback based approach, and not via coroutines. Correct?
i
Your coroutine context will not reliability survive through to completion
m
Got it, thanks @Ian Lake ๐Ÿ™‚
One thing though..I didn't get how to exactly pop off the current fragment (in my case, the selfie fragment) after I set the result ๐Ÿ˜
i
You didn't include how you added the selfie fragment, but you'd want to do the opposite of that ๐Ÿ™ƒ
๐Ÿ˜„ 1
m
๐Ÿ™‚ findNavController().navigate(FragmentADirections.actionToFragmentB()) Also, what if I'm using an earlier stable version? How do I do this?
i
Ah, then you
navController.popBackStack()
and can use Navigation's returning a result API, which is stable: https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_result
m
Thanks, but now the fragment is not popping off.
It keeps reappearing ๐Ÿ˜ž
i
Sounds like you're calling navigate within a LiveData
observe
block that is retriggering every time you go back to your other Fragment. LiveData isn't made for events
m
It's cool you guessed it right even without looking at the code. That's indeed the problem! So LiveData is only about updating UI based on changes in data? And normal method calls or callbacks are the only option in a case like mine?
i
You can certainly warp LiveData in unnatural ways to handle events ala https://link.medium.com/jfSrjj6i38
๐Ÿ‘๐Ÿผ 1
But yes, using something built for event streams like regular callbacks or
Flow
is perhaps the cleaner solution - kind of like using the right tool for the job
๐Ÿ™‚ 1
๐Ÿ‘๐Ÿผ 1
m
Thanks for the pointer to the article. Exactly covered my case ๐Ÿ˜„ Can you if possible point me to some guide about achieving the same using
Flow
?