https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
l

loloof64

12/15/2021, 12:09 PM
Reading at navigation documentation (https://developer.android.com/jetpack/compose/navigation#nav-with-args), I've seen that we can navigate with string argument. But is there a way to pass custom argument ? Or must I serialize/deserialize between my custom
class
and
String
? Or is even this last very bad practice ?
l

Leo Zam

12/15/2021, 1:20 PM
We just convert object to json and wrap to base64, i guess it’s only way
😳 1
l

loloof64

12/15/2021, 1:32 PM
Thank you 🙂 I am also thinking about handling the data directly in the main activity, where I define the
@Composable
: so to do state hoisting. I think i can handle that way also.
c

Colton Idle

12/15/2021, 3:01 PM
It's a bad practice to pass anything but strings. You should think of it like a web address. You want to give an id in the web address, not the full object serialized into a string. From the docs:
Caution: Passing complex data structures over arguments is considered an anti-pattern. Each destination should be responsible for loading UI data based on the minimum necessary information, such as item IDs. This simplifies process recreation and avoids potential data inconsistencies.
tldr. Just use identifiers. Bonus being you can retrieve stuff after process death. if you really want to "share" an object, just put the object in some shared scope by the two things that need it.
☝️ 1
👍🏾 1
i

Ian Lake

12/15/2021, 3:40 PM
Yep, identifiers make sense. Arguments shouldn't be the source of truth for data
🤩 1
👍🏾 1
That being said, Navigation supports passing custom types if your identifier is complicated (you absolutely shouldn't be base64ing anything) : https://developer.android.com/guide/navigation/navigation-kotlin-dsl#custom-types
2
l

loloof64

12/16/2021, 8:54 AM
@Colton Idle Thank you. I was not aware of this fact. In fact I need to pass a list of PGNGame, which is a custom type itself.
@Ian Lake Thank you. So I'll need to review Jetpack Compose concepts : as I'm still having some issue understanding the fact that argument should not be the source of truth for data. Also my PGNGame custom data is quite a rather complex data : hold a mutable map from String to String and a list of another custom data.
@Colton Idle I will also investigate into the sharing solution, from the top
Composable
in main activity
As my shared data is very very complex (with a recursive data), I went into sharing it with a view model. Not sure if this is fine. Also I'm afraid this could create Memory Leak
Is there a better way to share complex recursive data among NavHost components ?
c

Colton Idle

12/16/2021, 12:15 PM
I think its fine. yeah. lol.
👍🏾 1
you just gotta make sure you can get that data back if it goes away (process death)
l

loloof64

12/16/2021, 3:29 PM
No other way => so I'm gonna for it
👍 1
I'm sure I can always recover data : as in fact it is made from a Chess Pgn file format (either from assets, or from local files). So all I have to do is to parse the file again, and it's quite fast (using an ANTLR4 grammar). So yes, data can be easily recovered.
Finally came to make my custom data
@Parcelize
and to use this feature
val games = extractGames(fileData = it, context = context)
     navController.currentBackStackEntry?.savedStateHandle?.set(
       NavHostParcelizeArgs.gamesList,
       games
     )
navController.navigate(NavHostRoutes.gamePage)
and
val gamesList =
        navController.previousBackStackEntry?.savedStateHandle?.get<List<PGNGame>>(
            NavHostParcelizeArgs.gamesList
        )
and
@Composable
fun MainContent(stockfishLib: StockfishLib) {
    val navController = rememberNavController()
    NavHost(navController, startDestination = NavHostRoutes.gamesListPage) {
        composable(NavHostRoutes.gamesListPage) {
            GamesListPage(
                navController = navController,
            )
        }
        composable(NavHostRoutes.gamePage) {
                GamePage(
                    navController = navController,
                    stockfishLib = stockfishLib,
                )
        }
    }
}
This way should be far cleaner.
i

Ian Lake

12/16/2021, 11:55 PM
No, you really, really don't want to be abusing savedStateHandle for that. This is way, way worse than your initial code of having a
rememberSaveable
above the NavHost layer acting as a "repository" that both destinations use as their source of truth
👍🏾 1
I'd suggest reading the Guide to App Architecture, which talks specifically about how to get data onto your screens: https://developer.android.com/jetpack/guide
👍 1
👍🏾 1
l

loloof64

12/23/2021, 10:12 AM
Hi eveyone ! I've started to design my app with Jetpack Architecture components. But then I'm stuck about how to use my UseCase so that all screens can make a call to it. More in the following posts
This is the use case.
This is the data source.
This is the repository.
But how to "share" the use case to all screens ? I mean, where should I instantiate it ? In the
@composable
which defines the
NavHost
? And how to pass it to all screens ? In short, where should I create the single instance of
GamesFromFileExtractorUseCase
? Otherwise, should the
GamesFromFileExtractorUseCase
be a Singleton / object class ?
This is the current state of my main activity.
Hi! Is creating a Singleton of my use case this way fine ? Or is it bad practise ? (Trying to follow Android Architecture Guidelines)
j

Javier

12/23/2021, 10:54 AM
I wouldn't use a
object
+
lateinit var
if I could use a
class
+
@Singleton
+
val
👍🏾 1
l

loloof64

12/23/2021, 10:55 AM
Thank you. Could you give a simple snippet as an example ? Because I don't see yet how to apply your suggestion
Oups. Didn't realize that
@Singleton
is also a Dagger annotation. Thank you very much 🙂
Meanwhile, it seems that using
@Singleton
does not make it a Singleton : https://stackoverflow.com/a/44237507/662618
So maybe I should create it inside Application class instead : https://stackoverflow.com/a/57118576/662618
j

Javier

12/23/2021, 12:57 PM
depending on how you configure
@Singleton
, you should get the same instance always which is your use case
👍🏾 1
l

loloof64

12/23/2021, 2:29 PM
Thank you. So I'll try this way. Also the SO link I gave above was posted on 2019.
I'm still wondering if the following use of use case is good : to set my main use case instance inside the application class and reference to it in each page
Here I'm calling
(context.applicationContext as MyApplication).gamesFromFileExtractorUseCase.extractGame()
but I'm wondering whether it is better or worse than setting it inside NavHost state.