Yes, you can create a similar structure for your navigation classes in your initial code.
Create a sealed class Screen that represents your navigation screens.
kotlin
sealed class Screen(val route: String, val title: String) {
object Home : Screen("home", "Home")
object Favorites : Screen("favorites", "Favorites")
}
Modify the NavigationHost composable to use the new Screen sealed class.
kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DeezerMusicPlayerTheme {
// Set up navigation
val navController = rememberNavController()
Scaffold(
bottomBar = {
BottomNavigation {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
bottomNavigationItems.forEach { screen ->
BottomNavigationItem(
icon = { Icon(Icons.Default.Home, contentDescription = null) }, // Set your custom icons here
label = { Text(screen.title) },
selected = currentRoute == screen.route,
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.startDestinationId)
launchSingleTop = true
}
}
)
}
}
},
content = { paddingValues ->
val navHostController = navController as NavHostController
NavigationHost(
navController = navHostController,
modifier = Modifier.padding(paddingValues)
)
}
)
}
}
}
@Composable
fun NavigationHost(
navController: NavController,
modifier: Modifier = Modifier,
) {
val categoriesViewModel = remember { CategoriesViewModel() }
val categories by categoriesViewModel.categories.collectAsState()
if (navController is NavHostController) {
navController.setViewModelStore(ViewModelStore())
}
NavHost(navController = navController as NavHostController, startDestination = Screen.Home.route) {
composable(Screen.Home.route) {
HomeScreen(categories = categories, navController = navController, modifier = modifier)
}
composable(Screen.Favorites.route) {
FavoritesScreen(categories = categories, navController = navController, modifier = modifier)
}
}
}
@Composable
fun HomeScreen(
categories: List<Category>,
navController: NavController,
modifier: Modifier = Modifier
) {
// HomeScreen content goes here (not the Scaffold)
MusicCategoriesScreen(categories = categories)
}
@Composable
fun FavoritesScreen(categories: List<Category>, navController: NavController, modifier: Modifier = Modifier) {
}
} -> this is my MainActivity but bottom navigation bar and scaffold causes issues. -> these are another codes for bottom navigation and it is working. ->package com.talhaoz.loginbottombarapp.ui.bottombar
}. -> I want you to apply the second bottom navigation bar prencible to my initial code. So i can use it like this. Explain me how to do it and write the code for me
You can follow the second bottom navigation bar example and apply it to your initial code by making the following modifications:
Create BottomBar composable to refactor the bottom navigation content.
kotlin
@Composable
fun BottomBar(navController: NavController, bottomNavigationItems: List<Screen>) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
BottomNavigation() {
bottomNavigationItems.forEach { screen ->
BottomNavigationItem(
icon = { Icon(Icons.Default.Home, contentDescription = null) }, // Set your custom icons here
label = { Text(screen.title) },
selected = currentRoute == screen.route,
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.startDestinationId)
launchSingleTop = true
}
}
)
}
}
}
Simplify your Scaffold content by replacing the BottomNavigation logic with the new BottomBar.
Now you have a similar structure to the second bottom navigation bar example, and you can handle the navigation and state in a similar manner as it's done in the example.
Here's the complete MainActivity code after the modifications:
kotlin
class MainActivity : ComponentActivity() {
private val bottomNavigationItems = listOf(
Screen.Home,
Screen.Favorites
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DeezerMusicPlayerTheme {
val navController = rememberNavController()
Scaffold(
bottomBar = { BottomBar(navController, bottomNavigationItems) },
content = { paddingValues ->
val navHostController = navController as NavHostController
NavigationHost(
navController = navHostController,
modifier = Modifier.padding(paddingValues)
)
}
)
}
}
}
@Composable
fun BottomBar(navController: NavController, bottomNavigationItems: List<Screen>) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
BottomNavigation() {
bottomNavigationItems.forEach { screen ->
BottomNavigationItem(
icon = { Icon(Icons.Default.Home, contentDescription = null) }, // Set your custom icons here
label = { Text(screen.title) },
selected = currentRoute == screen.route,
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.startDestinationId)
launchSingleTop = true
}
}
)
}
}
}
// ... Rest of your code (NavigationHost, HomeScreen, FavoritesScreen, etc.)
}
With this code, you now have a similar structure to the example, and you can handle navigation and state as shown in the example.
sealed class NavigationScreen(val route: String, val title: String, @DrawableRes val icon: Int) {
object HomeScreen : NavigationScreen(
route = "home_screen",
title = "Home",
icon = R.drawable.ic_home
)
Now, your initial code has a similar structure to the second code with respect to handling navigation. Remember that these suggested modifications should be applied to your MainActivity file.