Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with LocalContext.current (@Composable invocations can only happen from the context of a @Composable function)

I’m currently developing the Home Screen for my app but I’ve run into an issue. The line val context = LocalContext.current is causing an error that states, "@Composable invocations can only happen from the context of a @Composable function". Could you provide any guidance on how to resolve this? To give you a better understanding, here’s the complete function:

@Composable
fun HomeScreen(navController: NavController) {
    // Use a Box to allow for layering of composables on top of each other
    // Used to keep the content at the bottom center of the screen.
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp), // Apply padding around the box
        contentAlignment = Alignment.BottomCenter // Align content to the bottom center
    ) {
        // Column to stack buttons vertically
        Column(
            // Center column contents horizontally
            horizontalAlignment = Alignment.CenterHorizontally,
            // Space between the column contents
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            // Button to NAVIGATE TO THE BENEFITS SCREEN
            Button(
                onClick = { navController.navigate("sleep_benefits") },
                // Apply padding to only the top of the first button to push it upwards
                modifier = Modifier.padding(bottom = 1.dp)
            ) {
                Text(text = "Learn About Sleep Benefits")
            }
            // Button for USER LOGIN
            Button(
                onClick = { /* TODO: Add navigation to login screen here */ }
            ) {
                Text(text = "Login to Track Sleep")
            }

            // New Sign Up Button
            Button(
                onClick = {
                    val context = LocalContext.current
                    context.startActivity(Intent(context, SignInActivity::class.java))
                },
                modifier = Modifier.padding(top = 8.dp)
            ) {
                Text(text = "Sign Up")
            }
        }
    }
}

I tried looking at this: Error: "@Composable invocations can only happen from the context of a @Composable function"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace:

            // New Sign Up Button
            Button(
                onClick = {
                    val context = LocalContext.current
                    context.startActivity(Intent(context, SignInActivity::class.java))
                },
                modifier = Modifier.padding(top = 8.dp)
            ) {
                Text(text = "Sign Up")
            }

with:

            val context = LocalContext.current

            // New Sign Up Button
            Button(
                onClick = {
                    context.startActivity(Intent(context, SignInActivity::class.java))
                },
                modifier = Modifier.padding(top = 8.dp)
            ) {
                Text(text = "Sign Up")
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading