Home > Software engineering >  Android Jetpack Compose Navigation doesn't work
Android Jetpack Compose Navigation doesn't work

Time:07-03

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.

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)
                }
            }
        }
    }
}

CodePudding user response:

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)
                }
            }
        }
    }
}
  • Related