when for example I want to redirect from my app to...
# compose
m
when for example I want to redirect from my app to SMS app with
Intent
, how do I do it in Compose?
z
The most extensible and testable way is to make your compose ignorant of the concept of `Intent`s, and instead just wire up event handler functions into the android-specific parts of your code, at which point the answer would be the same as for non-Compose code.
e.g.
Copy code
@Composable fun YourScreen(onLaunchSms: () -> Unit) {
  // …
  Button(onClick = onLaunchSms) { … }
}
and so on, up your composable tree, until you get to the top. Or, until you get to something like a view model:
Copy code
@Composable fun YourScreen() {
  val viewModel by viewModels()
  // …
  Button(onClick = viewModel::launchSms) { … }
}
👍 3
m
I thought maybe there is some other built in Compose idiomatic way. thanks
i
It is much more of a design decision on where you call
LocalContext.current.startActivity
- the point is that calling that is essentially untestable, so you'd want to ensure that low level composables delegate that call up as much as possible
👍 2
m
Done that, am kinda happy with my design right now 😄
t
@Ian Lake What would be the proper way to pass the activity to some of the view model calls for things that would still require it during migration. All I see is casting the LocalContext.current but maybe there's some more official ways?
m
@Tolriq there is
AndroidViewModel
if you need something like that. this is not unit testable. https://developer.android.com/reference/androidx/lifecycle/AndroidViewModel am not sure Compose will get rid of
Context
. you should be careful where you put these calls as Ian mentioned above
t
I'm talking about activity not application context. Many things will still rely on passing activity until the whole world support Compose.
m
you can start activity with application context too
t
Again not what I talk about. External libraries can require activity as argument for like dialogs. So the question does not change. What is the official way to get the activity from the composable. Since it's 90% related to your first question I jumped in.
👌 1
c
I think you would pass the event up of the click or whatever you're reacting to... lets say up to the AAC ViewModel, then the vide model would represent the dialog as either an event or state. If it's an event then it sends the event from VM to activity/fragment and then you just handle it there. If you want to model the dialog as state then idk. But maybe for a 3rd party dialog you might consider it an event.
t
Your solution implies some kind of shared state between compose scope and activities / fragment for the compose viewmodel to be able to talk to the activity view model. Currently compose viewmodels are not sharable natively with the activity like you could do with fragments to use a shared activity one.