icerockdev / moko-permissions

Runtime permissions controls for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
360 stars 36 forks source link

KMM Compose Android Error: "ClassCastException: com.example.app.MainActivity cannot be cast to androidx.fragment.app.FragmentActivity" #106

Closed Stonerub closed 4 months ago

Stonerub commented 6 months ago

Hi, got issue in kmm compose, on android, BindEffect() call this exception: ClassCastException: com.example.app.MainActivity cannot be cast to androidx.fragment.app.FragmentActivity

iOS works great, without errors. My compose code:

    val factory: PermissionsControllerFactory = rememberPermissionsControllerFactory()

    val viewModel = getViewModel(
        key = "app viewModel",
        factory = viewModelFactory { AppViewModel(
            databaseDriverFactory,
            factory.createPermissionsController()
        ) }
    )

    BindEffect(viewModel.permissionsController)

Using moko mvvm. ViewModel permission calling:

init {
        viewModelScope.launch {
            permissionsController.providePermission(Permission.REMOTE_NOTIFICATION)
        }
}
Tingel24 commented 6 months ago

I have the same problem

mojtaba-amiri commented 6 months ago

Apparently it's about the MainActivity being ComponentActivity and not FragmentActivity. Changing it to FragmentActivity would be a workaround until Moko team fixes the issue.

Stonerub commented 6 months ago

Apparently it's about the MainActivity being ComponentActivity and not FragmentActivity. Changing it to FragmentActivity would be a workaround until Moko team fixes the issue.

Thank's, it works for me

ZiXOps commented 5 months ago

For now developer must use FragmentActivity as root activity to be able display permission dialogs with fragmentManager. I think in future we can change request permissons approach to rememberLauncherForActivityResult for compose apps.