can I use async do IO in one activity, and bind th...
# android
i
can I use async do IO in one activity, and bind that Deferred object into a companion object, in another activity to do await for the IO result to update UI?
a
You would have to wait for the result of your async task before moving forward.
Actually, can you describe what your end goal is?
j
It sounds to me like yes you can but no you shouldn't. This would be abusing static fields leading to leaks most likely.
2
j
You may want to look at either using a Service or WorkManager
j
Or use single activity architecture and share your viewmodel
s
companion object is undoubtedly not the right approach
i
for example, there're MainActivity and SecondActivity, from MainActivity jump to SecondActivity, and SecondActivity do some IO and UI, but those IO take time too long, so I wonder if I can do IO in MainActivity with
async
, then get result with
await
in SecondActivity?
I have tried that, but when I use
await
I got "parent job is cancelling" exception
this is the code, and it does not have that problem anymore, weird, but it worked now. https://paste.ubuntu.com/p/pVwqQmDRJy/
j
Well it is still the wrong way of doing thigs. You are trying to do asynchronous work on UI level, which is a bad practice. There are amazing components and architectural patterns that can help you, but part of that is to actually learn to develop properly for Android. I advise to check out the Android code labs by google to get a hang of different topics, Workmanager is one of them (which might suit your needs here) https://developer.android.com/courses start with android basics and when you get the hang of it continue with the advanced android in kotlin.
1