2

I saw an issue in android compose navigation. I have a simple app with two screens, each containing a button to the other screen, each screen have a log statement in the begining. On pressing the button in either screen a recomposition pattern is being observed in the Logs and layout inspector.

I checked the layout inspector tab also, It looks like recomposition is not getting skipped, it happening all the time. There is no viewmodel or state in screens.

@Composable
fun Screen1(
    onNavigate2: () -> Unit,
) {
    Log.e("SCREEN", "Screen 1")
    Column(
        verticalArrangement = Arrangement.Bottom,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Screen 1")
        Spacer(modifier = Modifier.height(12.dp))
        Button(onClick = onNavigate2) {
            Text(text = "Screen 2")
        }
    }
}

@Composable
fun Screen2(
    onNavigate1: () -> Unit,
) {
    Log.e("SCREEN", "Screen 2")
    Column(
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Screen 2")
        Spacer(modifier = Modifier.height(12.dp))
        Button(onClick = onNavigate1) {
            Text(text = "Screen 1")
        }
    }
}

@Composable
fun AppNav() {
    val navController = rememberNavController()
    Log.e("AppNav", "AppNav")
    NavHost(
        modifier = Modifier.fillMaxSize(),
        navController = navController,
        startDestination = "screen1"
    ) {
        composable(route = "screen1") {
            Screen1(
                onNavigate2 = {
                    navController.navigate("screen2")
                }
            )
        }
        composable(route = "screen2") {
            Screen2(
                onNavigate1 = {
                    navController.navigate("screen1")
                }
            )
        }
    }
}

Logs :-

2024-03-06 15:23:01.588 28919-28919 PracticeTheme  E  PracticeTheme
2024-03-06 15:23:01.590 28919-28919 Surface        E  Surface
2024-03-06 15:23:01.590 28919-28919 AppNav         E  AppNav
2024-03-06 15:23:01.598 28919-28919 SCREEN         E  Screen 1

After pressing button to navigate to screen 2 :-

2024-03-06 15:23:24.654 28919-28919 SCREEN         E  Screen 1
2024-03-06 15:23:24.660 28919-28919 SCREEN         E  Screen 2
2024-03-06 15:23:24.686 28919-28919 SCREEN         E  Screen 1
2024-03-06 15:23:24.691 28919-28919 SCREEN         E  Screen 2
2024-03-06 15:23:25.389 28919-28919 SCREEN         E  Screen 2
2024-03-06 15:23:25.408 28919-28919 SCREEN         E  Screen 2
4
  • You are creating new screens (composables) during each navigation with static data. So, it is not reusing the same Composable. If you navigate back and forth N times. You have to press the back button N times to navigate to the initial screen. Commented Mar 6, 2024 at 11:05
  • 1
    Have you checked if the transition animations are responsible for these recompositions? Commented Mar 6, 2024 at 11:10
  • 1
    @Leviathan I checked and read it in the documentation, it says that the current composable will be recomposed for crossfade transition, but not sure why other recompositions are happening again. Commented Mar 7, 2024 at 14:31
  • 1
    @Abhimanyu I don't understand how static data is making recomposition on single navigation, can you please elaborate a little and how to avoid it. Commented Mar 7, 2024 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.