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

Mike S

07/20/2020, 2:00 AM
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

Gabriel Feo

07/20/2020, 2:03 AM
You could store a Deferred somewhere
m

Mike S

07/20/2020, 2:04 AM
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

ja.son

07/20/2020, 2:06 AM
1. Room & LiveData 2. Add Activity's ViewModel to Fragment
If I understand correctly!
m

Mike S

07/20/2020, 2:07 AM
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

ja.son

07/20/2020, 2:19 AM
아하!
wait a minute please!
👍 1
Copy code
ViewModelProviders.of(context)
->
ViewModelProviders.of(activity)
☝️ 1
👍 1
m

Mike S

07/20/2020, 2:22 AM
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

Ian Lake

07/20/2020, 2:39 AM
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

Emiliano Schiavone

07/20/2020, 12:30 PM
Passing the data with an Intent?
13 Views