I want to use Jetpack Compose in my App. I am already using Koin for DI. Because I have a lot of convenience methods in my BaseFragment I want to inherit from it and build the corresponding view with compose.
Now the Problem is that when using DI in the BaseFragment and inheriting from it the preview of the composable wont be shown and following error Message appears:
and following exception is thrown:
java.lang.IllegalStateException: KoinApplication has not been started
at org.koin.core.context.GlobalContext.get(GlobalContext.kt:36)
at org.koin.java.KoinJavaComponent.getKoin(KoinJavaComponent.kt:122)
at org.koin.java.KoinJavaComponent.get(KoinJavaComponent.kt:87)
at org.koin.java.KoinJavaComponent.get$default(KoinJavaComponent.kt:81)
at org.koin.java.KoinJavaComponent.get(KoinJavaComponent.kt)
...
My BaseFragment looks something like this
public abstract class BaseFragment {
private final ActiveViewIdInteractor activeViewIdInteractor =
new ActiveViewIdInteractor(KoinJavaComponent.get(ActiveViewIdService.class));
...
and my Fragment which inherits looks something like this
class ComposeDemoFragment: BaseFragment() {
...
@Composable
fun ComposeDemoFragmentContent() {
Text(text = "Hello World",
Modifier
.fillMaxWidth()
.background(Color.Cyan)
)
}
@Preview
@Composable
private fun Preview() {
ComposeDemoFragmentContent()
}
If using the exact same preview in a Fragment which doesn't inherit from BaseFragment everything works fine. I already included the dependency for "Koin for Compose" and also tried using CoKoin. At this Point I don't know what to do with the error Message or if the error Message is even barely related to the actual Problem.
Is this a Bug or is there a way to bypass this error?