In terms of the architecture what is the best way ...
# android-architecture
m
In terms of the architecture what is the best way to maintain global application state without going offline first approach? For example, In the application start up I need to make an API call and the response from that API should be available App wide (Ui and view models)
s
Just saving it at repository (singleton) level might help you. Or you could have a shared ViewModel but that might be more work.
s
If you're on a single activity compose-only app, you can save some of that state in something like NiaAppState does here https://github.com/android/nowinandroid/blob/main/app%2Fsrc%2Fmain%2Fkotlin%2Fcom%2Fgoogle%2Fsamples%2Fapps%2Fnowinandroid%2Fui%2FNiaAppState.kt We do that for some global state, and you can initialize it by running some network request. Just gotta define a default before that comes back or if it fails though of course.
m
We do have single activity compose-only app. The above solution is good if we wanna have state in composables but I want a way where we can just inject the response object in a viewmodel, repository or any class. Something like how we do in DI.
s
In that case go with object (singleton) class which will be available till app lifecycle and you can inject this class anywhere you required.
👍 4
j
I would most probably using WorkManager if not super critical have to run at certain point. And using a manager / repository cache/store value. Later on, observe changes in ui level like viewmodel get result from storage.
d
What's wrong with placing it in the Application class itself?