-
Hello! Thanks for your amazing work! I've got a question for you: How would you start activity with an Intent (e.g. to open a map or dialer) in KMM project using Decompose with Jetpack Compose for Android (at least for now 👀). Would you just start activity from @composable function by passing LocalContext.current or delegate it to a Store from the Component? How would you retrieve the result back? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
As for now, I just pass the intent as a callback (which can be called later) when inflating your component in Android's Activity. I wonder if there is a better approach for this. |
Beta Was this translation helpful? Give feedback.
-
Personally I like explicity and compile-time safety. So I would go with the following solution: // In module X
class ComponentX(
componentContext: ComponentContext,
private val startCamera: (CameraParams) -> Unit
) : ComponentContext by componentContext {
fun onCameraResult(fileName: String) {
}
}
class CameraParams // In module Y
class ComponentY(
componentContext: ComponentContext,
private val startCamera: (CameraParams) -> Unit,
private val startBrowser: (url: String) -> Unit
) : ComponentContext by componentContext {
fun onCameraResult(fileName: String) {
// Deliver the result to ComponentX
}
}
class CameraParams // In module Z
class ComponentZ(
componentContext: ComponentContext,
private val startCamera: (CameraParams) -> Unit,
private val startBrowser: (url: String) -> Unit,
private val startFileChooser: () -> Unit
) : ComponentContext by componentContext {
fun onCameraResult(fileName: String) {
// Deliver the result to ComponentY
}
fun onFileChooserResult(fileName: String) {
}
}
class CameraParams // In module Root
class RootComponent(
componentContext: ComponentContext,
private val startCamera: (CameraParams) -> Unit,
private val startBrowser: (url: String) -> Unit,
private val startFileChooser: () -> Unit
) : ComponentContext by componentContext {
fun onCameraResult(fileName: String) {
// Deliver the result to ComponentZ
}
fun onFileChooserResult(fileName: String) {
// Deliver the result to ComponentZ
}
}
class CameraParams Such approach clearly indicates what screens can be launched by the whole tree or a sub-tree. You must satisfy all dependencies, otherwise it won't compile. This is specially important when you want to reuse a component, with all its children. E.g. when you include Later, if for example you will have // In module Root
class RootComponent(
componentContext: ComponentContext,
private val startBrowser: (url: String) -> Unit,
private val startFileChooser: () -> Unit
) : ComponentContext by componentContext {
fun onFileChooserResult(fileName: String) {
// Deliver the result to ComponentZ
}
// Handle camera requests from the ComponentZ using CameraComponent
} And so it will become single activity in this case. |
Beta Was this translation helpful? Give feedback.
Personally I like explicity and compile-time safety. So I would go with the following solution: