https://kotlinlang.org logo
k

KotlinLeaner

09/21/2022, 11:39 AM
Hi guys I am learning side-effect in my project. I want to know when should I use
LaunchedEffect
and
SideEffect
in which scenario. I am adding some piece of code using both effect. Please lemme know if I am doing wrong here.
1st using LaunchedEffect, please guide me if we need effect on this function or not.
Copy code
@Composable
fun BluetoothRequestContinue(multiplePermissionsState: MultiplePermissionsState) {
    var launchPermission by remember { mutableStateOf(false) }
    if (launchPermission) {
        LaunchedEffect(Unit) {
            multiplePermissionsState.launchMultiplePermissionRequest()
        }
    }
    AbcMaterialButton(
        text = stringResource(R.string.continue_text),
        spacerHeight = 10.dp
    ) {
        launchPermission = true
    }
}
2nd using SideEffect to open setting using intent
Copy code
@Composable
fun OpenPermissionSetting(router: Router = get()) {
    val activity = LocalContext.current as Activity
    var launchSetting by remember { mutableStateOf(false) }
    if (launchSetting) {
        SideEffect {
            activity.startActivity(router.permission.getPermissionSettingsIntent(activity))
        }
    }
    AbcMaterialButton(
        text = stringResource(R.string.open_settings),
        spacerHeight = 10.dp
    ) {
        launchSetting = true

    }
}
Please let me know if we need Effect or not. Also guide me if we need different effect as well. Thanks
k

KotlinLeaner

09/21/2022, 11:43 AM
@Filip Wiesner I am confused through the doc.
That's why I don't understand should I use the side-effect or not..
f

Filip Wiesner

09/21/2022, 11:48 AM
You don't Effect API here at all. You can launch the permission from the button
onClick
k

KotlinLeaner

09/21/2022, 11:49 AM
Okk got it now.. Thanks
f

Filip Wiesner

09/21/2022, 11:51 AM
I assume you are using Accompanist permission library. There is code example
k

KotlinLeaner

09/21/2022, 11:51 AM
Another question in 2nd example
I am opening the setting of my application that would be use side effect?
f

Filip Wiesner

09/21/2022, 11:54 AM
It's the basically the same question with the same answer 🙂
k

KotlinLeaner

09/21/2022, 11:54 AM
okk Thank you so much...
3 Views