Hello everyone, I want to fire off a REST API cal...
# android
m
Hello everyone, I want to fire off a REST API call with a Kotlin Coroutine from one fragment then handle the response in a different activity. Any recommendations? I feel like a broadcast receiver and service implementation is too heavy weight for what I'm trying to do.
g
You could store a Deferred somewhere
m
Thanks Gabriel. Ahhh... that way i can decouple the API service call from the fragment im trying to close immediately. How can I give you a point for that? isn't it like a command and your username?
j
1. Room & LiveData 2. Add Activity's ViewModel to Fragment
If I understand correctly!
m
Oh wow. i could do that too. Thank you! I dont think kotlin slack has a point granting system btw
I think the ViewModelProvider dies when the starting fragment dies though.
If I start a viewmodel function from one fragment. Kill it. Then try to catch the response in a viewmodel observer from a different activity, I don't think it will work.
j
아하!
wait a minute please!
👍 1
Copy code
ViewModelProviders.of(context)
->
ViewModelProviders.of(activity)
☝️ 1
👍 1
m
I think I could also isolate the REST HTTP call response. And defer it too the other activity. And show a toast when the deferred response comes in.
i
As per the Single Activity talk (

https://youtu.be/2k8x8V77CrU

), there is no good way to do this. Activities really are separate elements from one another with no nicely shared scope for this type of thing. That's why building those scopes within a single activity works so much better
✍️ 4
❤️ 1
e
Passing the data with an Intent?