Hello, I come from a background of iOS (SwiftUI) p...
# compose
c
Hello, I come from a background of iOS (SwiftUI) programming and I’m very new to Android and Jetpack Compose. I was wondering how Navigation is supposed to work in Compose. Specifically, I’m wondering how you’re supposed to send a lot of data from one screen to another. Let’s say for example that I have a screen with a list of all natural parks in the US. I fetched this list from my backend, and each park is a Park object with many details. When the user selects a park, the app navigates to a park detail screen featuring all the park informations. Now, I’ve looked at the documentation, and it looks like we can send arguments to the next screen. But these arguments look like they were designed for small strings such as an id. Not for sending a whole object. I saw someone on StackOverflow say that you could serialize the Park object into a JSON string and send it as an argument. But this doesn’t feel like a good practice and I’m wary of using it. What is the correct thing to do in this case? Multiple ideas came to mind, but I don’t know which one is best: A. Send a serialized JSON string as a navigation argument B. Send only the park’s id and fetch the park once again in the detail screen. Whether by making a brand new request or fetching it from a cache C. Pass down a view model of some sort to the detail screen. The view model being shared between the list and the detail. D. Something I didn’t think about? Thank you in advance for your help!
m
yeish. You use compose navigation you have to deal with it 😛 My advice, do not use it, or use some other library that does that for you like raamcosta https://github.com/raamcosta/compose-destinations
a
If that's really a lot of information for each park, then you have to either store it in a database, or fetch from the backend. The amount of information you can send is limited to ~500 Kb per process, if I remember correctly. None of the libraries will help with big amounts of data. For relatively small data you can try libs, e.g. https://github.com/arkivanov/Decompose
c
Wow! I didn’t expect the solution was to use a third-party library for this 😄 It looks like Compose Destinations is actually serializing parameters under the hood… I’ll definitely look into it since it solves other issues I had with the standard compose navigation (type-unsafe navigation using strings) The Park objects aren’t supposed to be that big. They will mainly be plain text, with any asset simply being represented with URLs. I’d be surprised if a park could reach the limit, but you never know. However, using a database would be a lot of work. Even more so since I already have a comprehensive caching mechanism. I’m using GraphQL, with SQLite local storage and cache normalization. So making a new network request is actually feasible without much of a penaly. Maybe I should go with option B then?
a
You have to use a library for navigation anyway. There is a first-party library - AndroidX navigation-compose, and many third-party, e.g. those mentioned above. Libraries that allow passing objects between destinations have to always serialize the data at some point. Because of the Android configuration changes or possible process death, so the navigation stack is preserved and all the data is restored. If you think your objects are not big, then you can try a third-party library.
c
Alright, thank you very much for the advice 😊
m
If you have a pure Compose app you can just switch off all these configuration changes in the manifest. After that life is much easier.
c
@Michael Paus I’m actually pursuing the road of the pure compose app. My app just has a single activity and everything inside is just Compose for now. I’m sorry that I’m not experienced enough to know what these configuration changes are, but what would be the actual benefit from switching them off?
m
The benefits are, e.g, that your activity is not restarted anymore when you just rotate your device. Compose handles that for you and you do not constantly have to serialise/deserialise things.
🙏 1
I just keep my model in memory and that works nicely so far.
a
How does it solve the issue with passing classes between screens?
m
Here is a fragment of my manifest:
Copy code
android:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode">
@Arkadii Ivanov I use a centralised model in my app and I never had the need to pass any data between screens.
a
Ah alright thanks! That's indeed a solution with its pros and cons.
m
Could you give a few examples of the “cons”? I have to admit that I primarily have a desktop background and I often do not understand why Android programmers often do things in so complicated ways. I never felt the need for such things on desktop. One reason are these config changes but when you can get rid of them I don’t see any other reasons.
a
@Michael Paus the biggest one is the possible process death while the app is in the background. The aggressiveness may vary between device models/brands. And on some low-cost devices it's quite likely that your app will be killed when you are taking a picture or recording a video using e.g. the system camera app. Or the user may switch to a memory consuming app (a game) for a moment and then go back to your app, and boom - your stack is reset and the user lands to the initial screen. Another possible downside is that you have to maintain the back stack on your own. E.g. you normally will have to preserve the UI state when switching screens. A use case - you scroll a list, pick an item, a details screen shows, you close the details screen - the scrolling position usually should remain unchanged. Also, a centralized model may lead to a coupling between screens, which may be less scalable in medium/large projects.
m
These are some interesting points to look at although I have not yet suffered from them and I think that most of them could be solved in a more light-weight fashion. But I do not want to conquer this thread 😉.
❤️ 1
i
Note that on Android 12+, you cannot ever catch all config changes - a wallpaper change will force your activity to go through a config change (to update the dynamic colors, even if your app doesn't actually read them), no matter what flags you have set (it ends up going through a completely different system). This effectively means you must always handle config changes
☝️ 1
The correct is, as per many previous threads, is that both screens should be talking to a single, observable source of truth (e.g., a repository layer): https://kotlinlang.slack.com/archives/CJLTWPH7S/p1657897289085069?thread_ts=1657844159.552379&cid=CJLTWPH7S
c
@Clément Cardonnel the best way I like to think about these things are as scopes. So if you have an app that is single activity and two Composable screens (ScreenA and ScreenB), then you can have a ScreenAViewModel and ScreenBViewModel. which are scoped as expected to just their screen, and then you find yourself in the perdicament you are in now. BUT there are still two more scopes available to you to store that data. 1. The navHost scope/activity scope. These are effectively the same thing, but you can just create a ViewModel for your navHost or for your Activity, and then put any shared data in there. 2. The application scope which is overarching to your entire application. This is where I like to put data I need on every screen. Like my User model, but on certain apps where a lot of the screens use the same list of data then I might also put it there.
i
Very rarely does your Park object exist solely at the UI layer, which would be the only case where using a static argument as your source of truth would make sense
c
@Ian Lake with a repository layer solution, that repository is effectively application scoped, correct?
i
That depends entirely on your DI setup, but that is indeed the most obvious way to set things up
👍 1
m
An application scoped repository layer was what I basically meant when I said that I use a centralised model.
a
@Michael Paus please note, that the repository should be persistent in this case. And I believe it still implies that the navigation stack and arguments are persistent as well. With this approach you just pass an item id, and another screen loads all the data from the repository.
m
Of course. At some point you have to persist whatever needs to be persisted in case the app is terminated. I distinguish though between the UI state and the model state. In my specific case I do not care much when I should loose the UI state (a use-case specific decision) and I just start from the beginning by reading the persisted model state. But I just don’t see why I should go through the whole persistence cycle with some weird serialisation as mentioned above just because I am going from one screen to the other, which is a much more frequent action. Note, in Compose terms a screen is not the same as an activity. I assume a single activity Compose app here. When going from one screen to the other the needed data is just taken from the model which hopefully has that cached and does not have to load it again.
i
that makes some big assumptions - your app can and will go through config changes or process death and recreation when on any screen of your app (think: the user quickly opens the camera to take a picture of their kid, then comes back to your app - tada - process death and recreation). Each screen needs to be able to restore itself from saved instance state (the small bits of data ala the IDs you are supposed to be passing) and persisted / re-queried from scratch data
c
I'm happy Android studio added the button back to quickly test process death. 😁
m
I don’t argue against saving the UI state if you need to. There are means to do that and it is in general just a small amount of data but I do not see any reason to push large amounts of data around for every single screen change as the original author of this thread suggested. Especially not on limited devices.