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

Android Jetpack Compose Navigation doesn't work

I’m making an android app using Jetpack Compose and I have a problem. I used navigation to change the screen. I wanted to change firstRunLayout to loginLayout. You can see that i put that log.d code in order to check if this worked and I found out that logcat prints that successfully. I want to change the screen when I navigate. Is there something wrong with my code?

@Composable
fun setUpNavHost(navController: NavHostController) {
    NavHost(navController = navController, startDestination = Screen.FirstRun.route) {
        composable(route = Screen.FirstRun.route) {
            firstRunLayout(navController)
            Log.d("NavHost", "FirstRun")
        }
        composable(route = Screen.Login.route) {
            loginLayout()
            Log.d("NavHost", "Login")
        }
    }
}

p.s. I checked that there are Composable annotations at those layouts.

Button(onClick = {
                    Log.d("FirstRunActivity", "Button clicked")
                    navController.navigate(route = Screen.Login.route)
                }, colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xFFEF4746)), modifier = Modifier
                    .height(40.dp)
                    .width(300.dp), shape = RoundedCornerShape(50)) {
                    Text(text = "Continue", fontSize = 15.sp, color = Color.White)
                }

I used the button to navigate. You can see navController.navigate above. Also, this is the way that this navController was declared.

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

class FirstRunActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MemorizableTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = Color(0xFFEF4746)
                ) {
                    val navController = rememberNavController()
                    setUpNavHost(navController = navController)
                    
                    firstRunLayout(navController)
                }
            }
        }
    }
}

>Solution :

The problem is in your Activity, you call the navHost, then you call the firstRunLayout screen, so the firstRunLayout screen will be on the top of the navHost, you need to delete the call of firstRunLayout in your activity, so your code should look like this:

class FirstRunActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MemorizableTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = Color(0xFFEF4746)
                ) {
                    val navController = rememberNavController()
                    setUpNavHost(navController = navController)
                }
            }
        }
    }
}
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