https://kotlinlang.org logo
b

Bruno_

11/05/2019, 7:15 PM
is there any way to to load a file other than
startActivityForResult
? technically I could make everything that I need global, but it would cause spaghetti code
b

bezrukov

11/05/2019, 7:17 PM
1. Is it related to compose? 2. Is it related to kotlin?
b

Bruno_

11/05/2019, 7:22 PM
I think so, because that's the only way I know of which won't work in compose cus it doesn't use activities (which is required to use startActivityForResult)
am using something like that after all
Copy code
val context = +ambient(ContextAmbient)
context.startActivity(Intent(context, CustomActivityClass::class.java))
CustomActivityClass
has implemented
onActivityResult
which listens for a file
don't like the way I solved it cause will have to store the file as a global state but can't think of anything else
m

Michal Bacik

11/05/2019, 8:19 PM
Load file from uri:
context.contentResolver.openInputStream(uri)?.use { it.readBytes() }
Why would you start new activity for that?
b

Bruno_

11/05/2019, 8:22 PM
well it works nicely as long as you have the uri, the thing is how do I obtain it
it's my bad because I didn't mention it - I want to be able to pick a file from file browser (that's what I mean by
load a file
)
m

Michal Bacik

11/05/2019, 8:25 PM
Maybe change question to "how to call other activity from Compose and get result".
👆 1
b

Bruno_

11/05/2019, 8:36 PM
Anyway, if anyone would like to launch a file picker, that's more or less it: https://pastebin.com/embed_iframe/DAgXkFi7 probably not the best solution as I'm new to android, but it gets the job done
d

david.bilik

11/05/2019, 8:48 PM
i still cant see how is that related to Compose or Kotlin
b

Bruno_

11/05/2019, 9:09 PM
isnt it written in kotlin nor using compose? I just assumed that there must be other, compose-specific way to do such thing (and by that I mean a component, like
TopAppBar
), turned out there is not even tho one would help
I'm not using anything compose specific in that pastebin snippet, but it's made for composable functions, you just have to provide context
r

Ryan Mentley

11/05/2019, 9:36 PM
Compose doesn't have a dedicated navigation solution yet, but we're planning to have one at some point