Home > Software design >  Navigate from IconButton in an item in Lazycolumn
Navigate from IconButton in an item in Lazycolumn

Time:01-30

I want to navigate to another screen after clicking on the IconButton in TaskItem.kt but it doesn't work. Logcat doesn't show anything, there aren't any errors in "Run". After clicking the edit IconButton it should navigate to another screen.

enter image description here

Screen.kt:

package com.example.planner

sealed class Screen(val route: String) {
    object TasksList: Screen(route = "tasks_list")
    object AddScreen: Screen(route = "add_screen")
    object UpdateScreen: Screen(route = "update_screen")
}

NavGraph.kt:

package com.example.planner

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable

@Composable
fun Navigation(navController: NavHostController) {
    NavHost(navController = navController, startDestination = Screen.TasksList.route)
    {
        composable(
            route = Screen.TasksList.route
        ) {
            TasksList(navController = navController)
        }
        composable(
            route = Screen.AddScreen.route
        ) {
            AddScreen(navController = navController)
        }
        composable(
            route = Screen.UpdateScreen.route
        ) {
            UpdateScreen(navController = navController)
        }
    }
}

Part of TaskItem.kt:

IconButton(onClick = { navController.navigate(route = Screen.UpdateScreen.route) }, modifier = Modifier.size(36.dp)) {
                    Icon(
                        imageVector = Icons.Filled.Edit,
                        contentDescription = "Edit",
                        modifier = Modifier.padding(horizontal = 2.dp),
                        tint = MaterialTheme.colorScheme.onBackground
                    )
                }

MainActivity.kt:

class MainActivity : ComponentActivity() {

    private lateinit var navController: NavHostController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            PlannerTheme {

                // A surface container using the "background" color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    navController = rememberNavController()
                    Navigation(navController = navController)
                    MyApp(Modifier.fillMaxSize(), navController)
                }
            }
        }
    }
}

@Composable
fun MyApp(modifier: Modifier = Modifier, navController: NavController) {
    Surface(modifier, color = MaterialTheme.colorScheme.background) {
        TasksList(navController = navController)
    }
}

@Preview(name = "LightMode", showBackground = true)
@Preview(name = "DarkMode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(name = "FullPreview", showBackground = true, showSystemUi = true)
@Composable
fun DefaultPreview() {
    PlannerTheme {
        Surface(modifier = Modifier, color = MaterialTheme.colorScheme.background) {
            TasksList(navController = rememberNavController())
        }
    }
}

CodePudding user response:

MyApp(Modifier.fillMaxSize(), navController),Is this sentence redundant? Just remove it.

  • Related