android - How to prevent ModalBottomSheet content from loading only once in Jetpack Compose? - Stack Overflow

On one screen I'm using a ModalBottomSheet to play text to audio using TTS.I'm delaying gett

On one screen I'm using a ModalBottomSheet to play text to audio using TTS.

I'm delaying getting the text to read until the ModalBottomSheet is displayed.

The code works, but if I hide the ModalBottomSheet and click the button again to display it, the part of the code that fetches the text to read is executed again.

How can I fetch the content to read only once and only change it when the content changes?

    if (showBottomSheet) {
        ModalBottomSheet(
            onDismissRequest = {
                showBottomSheet = false
            },
            sheetState = sheetState,

            content = {
                analyticsHelper.logUniversalisTtsEvent(universalisResource.title)
                val read = universalisBodyForRead(universalis, typusId, userData)
                viewModelTts.loadData(read.text)
                ScreenTtsPlayer(viewModelTts)
            },
        ) 
    }

I've tried LaunchedEffect, but it doesn't work for me.

On one screen I'm using a ModalBottomSheet to play text to audio using TTS.

I'm delaying getting the text to read until the ModalBottomSheet is displayed.

The code works, but if I hide the ModalBottomSheet and click the button again to display it, the part of the code that fetches the text to read is executed again.

How can I fetch the content to read only once and only change it when the content changes?

    if (showBottomSheet) {
        ModalBottomSheet(
            onDismissRequest = {
                showBottomSheet = false
            },
            sheetState = sheetState,

            content = {
                analyticsHelper.logUniversalisTtsEvent(universalisResource.title)
                val read = universalisBodyForRead(universalis, typusId, userData)
                viewModelTts.loadData(read.text)
                ScreenTtsPlayer(viewModelTts)
            },
        ) 
    }

I've tried LaunchedEffect, but it doesn't work for me.

Share Improve this question asked Mar 5 at 23:08 A. CedanoA. Cedano 1,00117 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

the read and loadData calls are side effects and it will be triggered every time the composable recomposed not only when you dismiss and reopen the dialog . why LaunchedEffect didn't help you ? just with launchedEffect you told the compose compiler you need to execute this one time at the first composition so this will never execute again with every recomposition but when you dismis and reopen the dialog this is a new composition so it will execute again . so ? the best option for me is that make viewModel for this dialog and in the init of this dialog call your methods .... what if one of them you need to call again and again with every dialog open ? just use the launchedEffect(Unit)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745003269a4605628.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信