I have a single activity app and i need to call a ...
# compose-android
c
I have a single activity app and i need to call a network call when the app is opened. The current spec is to make the call when the process is started, so Activity onCreate makes sense, but since I'm trying to do this in a compose way I think I see two options: 1. Create an AAC VM for my activity (i dont have one yet) and do the work there so that its scoped to the activity or 2. Create a VM for my top level navHost and do the work there Anyone have reccomendations?
s
Do you want this work to persist (and finish on its own) if your app closes immediately after opening? Do you want this effect to still run if you deep link somewhere deeper and your start destination does not show at all when the app starts? Do you care if it runs multiple times, aka each time you open your app instead of only once? Do you care if it runs when you open the app on a cold boot vs a warm boot?
m
Out of pure curiosity, if the spec is on process started, why on on the App's oncreate?
t
Naivest noob side question for the day... VM = ViewModel in this context right? And not VirtualMachine because that wouldn't make any sense...
👌 1
c
Yes! AAC VM == android arch component viewmodel
Out of pure curiosity, if the spec is on process started, why on on the App's oncreate?
Apps onCreate could work, but I'd like to do the work in a ViewModel because I already know how to inject all of my dependencies into that via hilt
t
Duck typed acronyms during the era of the rise of manifest typing. What could possibly be more ironic.
m
If you already have hilt injecting your repository/service into thje app should be a piece of cake
If still not an option I dont have a lot of exp with compose nav so I'd go with good ol AACVM
c
Do you want this work to persist (and finish on its own) if your app closes immediately after opening?
Do you want this effect to still run if you deep link somewhere deeper and your start destination does not show at all when the app starts?
Do you care if it runs multiple times, aka each time you open your app instead of only once?
Do you care if it runs when you open the app on a cold boot vs a warm boot? (edited)
all good questions @Stylianos Gakis but for right now just a general "we should get some data when the app is first opened" is loose enough where we can sort of define what makes sense for us at the moment. we will refactor later if there are more solid specs. but for now. everytime the app or activity is created would be fine. onResume of the activity would be too often.
s
Then Application onCreate sounds good enough. If you don’t want to inject in your application for some reason, start a WorkManager job onCreate, and inject the things to it instead. I think you’d want it to be an expedited job for it to run asap
c
Yeah, I can do it in AppCreate, but I'm trying to stick to compose land, hence why I thought maybe a VM scoped to the navHost might be something folks would rec.
m
What I dont get its why force ting it Compose/Ui If the spec itself its scoped to the process?
s
a VM scoped to the navHost
Sure, if you make sure to scope it to the root route (aka global, just like if you injected it to your Activity) it would work, but feels like an overkill imo
âž• 1
👌 1
f
what about the splash screen and holding the UI until your data has loaded?
c
@Stylianos Gakis yeah I've never scoped to the parent before. ill try to see if that is somehow difficult.
@Francesc the data is required. its more of a best effort sort of thing, hence the lax rules on it.
f
you mean "not required"?
then maybe consider work manager, if you don't need it to start the app
âž• 1
m
Why not app initialisers then? Altho I dont remember if you can inject those
c
@Francesc, sorry yeah. "the data isn't* required"
f
then, if you don't need this as a requirement to start the app, I would strongly suggest you look at work manager. If you want this to be sent immediately as the app starts, you can use an expedited job
âž• 1
app initializers, as suggested above, is another alternative https://developer.android.com/topic/libraries/app-startup#startup-initialize