Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot go with intent from composable function of one activity to another activity

I have two activities that have different functions that navigate on them, but how to navigate to the function that it's not a part of that activity. I cannot navigate using intents, also with navigate cannot navigate because the functions are a part of different activities.

I want to move from a function that is a part of StartActivity to MainActivity but i got the error : @Composable invocations can only happen from the context of a @Composable function

Here is my main activity:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val startViewModel: StartViewModel by viewModels()
    private val viewModel: TabBarViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContent {
            val navController: NavHostController = rememberNavController()
            NavHost(
                navController = navController,
                startDestination = Screen.Introduction.route
            ) {
                composable(Screen.BottomNavigationScreen.route) {
                    BottomNavigationScreen()
                }


            }
        }
    }
}

Another Activity

@AndroidEntryPoint
class StartActivity : ComponentActivity() {
    private val viewModel: StartViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val startDestination = if (AppPreferences.hasSeenIntro) {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            return
        } else if (AppPreferences.hasDownloadedContent) {
            Screen.Introduction.route
        } else {
            Screen.SelectLanguage.route
        }

        setContent {
            val navController: NavHostController = rememberNavController()
            NavHost(
                navController = navController,
                startDestination = startDestination
            ) {
                composable(Screen.Start.route) {
                    Start(navController, viewModel)
                }
                composable(Screen.CheckWhere.route) {
                    CheckWheretoGo(navController)
                }
                composable(Screen.SelectLanguage.route) {
                    SelectLanguage(navController)
                }
                composable(Screen.Introduction.route) {
                    IntroductionScreen(navController)
                }

            }
        }
    }

Now inside my Introduction composable function :

@Composable
Introduction(){
                IconButton(onClick = {
                    if (selectedPosition + 1 == items.size) {
                        val context = LocalContext.current
                        val intent = Intent(context, MainActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}



like image 343
Fjolla Engjellushe Avatar asked Oct 12 '25 23:10

Fjolla Engjellushe


1 Answers

Replace:

@Composable
Introduction() {
    IconButton(
        onClick = {
            if (selectedPosition + 1 == items.size) {
                val context = LocalContext.current
                val intent = Intent(context, MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
        }
    )
}

with this:

@Composable
Introduction() {
    val context = LocalContext.current

    IconButton(
        onClick = {
            if (selectedPosition + 1 == items.size) {
                val intent = Intent(context, MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
        }
    )
}

You can only reference a composition local value, like LocalContext.current, from inside of a composable function or lambda/function type.
The onClick lambda/function type specifically is not composable. However, you can hoist the composition local read to be outside of IconButton() itself.

like image 87
CommonsWare Avatar answered Oct 14 '25 16:10

CommonsWare