I'm getting some really bizarre behaviour in an an...
# compose
c
I'm getting some really bizarre behaviour in an android application where I'm trying to integrate compose. It seems like a nice way to dip our toe in is to use
ComposeView
inside a
Fragment
- it works well with our current navigation system, but we're noticing things like
TextField
will not update consistently on text entry. The exact same composable in a
ComponentActivity
works as expected. Is there some particular stuff I need to be wary of when using JC with Fragments?
s
You could add some code snippet of what you’re trying to do and/or explain a bit more what “consistently” means in your context. Does it never update? Some times? 99% of the times?
c
yeah, sorry that's a good idea- unfortunately a code snippet is a little out of the question for now, but a better description of the observations: The first character being typed in will always be rendered, second character maybe 80% of the time, third character around 5% of the time. By placing a
Log.d
in, I can see that onValueChange IS being called and the value that should be rendered is being stored, and in fact, UI tests seem to think everything is hunky dory (indicating that the underlying semantic values are correct? Don't know, still new to compose 😞 ). Currently using compose version 1.0.5 and Kotlin 1.5.31, and it's not trivial to upgrade to 1.6.10 and 1.1.0 as it's an old codebase with maybe 200+ fragments all using android synthetics that are subject to compiler issues in kotlin 1.6 I believe
s
Yeah this doesn’t sound like something that would be fixed with 1.6.10 anyway. If I were you I’d try to make a minimum repro to this to see when it acts up. For example does this also happen when you have the simplest composable possible in that same place? Something like this. If it still acts weird, make a new project and try and repro it again. If it does happen, then make that sample project with compose 1.1.0 to see if that helps. Without any code I think this is the best I can think of since I don’t know what else could be going wrong
c
I'll try my best to get a github repo with it in - I was genuinely hoping there was a 'oh yeah if you're using fragments make sure you call xyz first' but wish me luck!
s
Haha yeah nothing that comes to my mind at least. Maybe also consider looking into this and
setViewCompositionStrategy(DisposeOnViewTreeLifecycleDestroyed)
. That’s the only “weird” thing we’ve had to do on the interop of these systems
c
ooooo mysterious code snippet homer drool - I'll give it a go
out of interest - I'm just returning a compose view in the following way:
Copy code
override fun onCreateView(...) {
   return ComposeView(requireContext()).apply {
      setContent {
         // Compose magic
      }
   }
}
That's legit right?
s
Yeah we’re doing the same, with the addition of a
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
in there
c
cheers man, really appreciate that!
🙌 1
after a good few hours trying to replicate everything- I've been unsuccessful- I can't get it to not work. It's like I'm trapped in some kind of bizarro-verse. Cheers for the help anywho Styllanos, wish me luck in the future
s
Haha we’ve all been there my friend 😄 I hope you manage to figure it out eventually.