package com.example.musicapp.Interface import MusicCategoriesScreen import TopBar import…
- User
package com.example.musicapp.Interface
import MusicCategoriesScreen import TopBar import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import com.example.musicapp.ui.theme.MusicAppTheme import com.example.musicapp.R import com.example.musicapp.ViewModel.CategoriesViewModel import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MusicAppTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { MainScreen() } } } } } @Composable fun FirstScreen(modifier: Modifier = Modifier) { val categoriesViewModel: CategoriesViewModel = viewModel() val categories by categoriesViewModel.categories.collectAsState() Scaffold( content = { padding -> Column( modifier = modifier.padding(padding) ) { // Add your UI components here MusicCategoriesScreen(categories = categories, topBar = { TopBar(title = "Music Categories") }) } } ) }
@Composable fun SecondScreen(modifier: Modifier = Modifier) { Scaffold( topBar = { // No top bar title }, content = { padding -> Column( modifier = modifier.padding(padding) ) { Text(text = "This is the second screen") } } ) }
/* @Composable fun MainScreen() { val selectedScreen = remember { mutableStateOf(0) } Scaffold( bottomBar = { BottomBar(selectedScreen.value, onItemSelected = { index -> selectedScreen.value = index }) }, content = { padding -> when (selectedScreen.value) { 0 -> FirstScreen(modifier = Modifier.padding(padding)) 1 -> SecondScreen(modifier = Modifier.padding(padding)) } } ) } */
@Composable fun MainScreen() { val navController = rememberNavController()
NavHost(navController, startDestination = "musicCategories") { composable("musicCategories") { val categoriesViewModel: CategoriesViewModel = viewModel() val categories by categoriesViewModel.categories.collectAsState() MusicCategoriesScreen( categories = categories, onCategorySelected = { category -> navController.navigate("artistsDetail/${category.id}") }, topBar = { TopBar(title = "Music Categories") } ) } composable("artistsDetail/{categoryId}") { backStackEntry -> val categoryId = backStackEntry.arguments?.getString("categoryId")?.toIntOrNull() if (categoryId != null) { ArtistsDetailScreen(navController, categoryId) } } }}
@Composable fun BottomBar(selectedScreen: Int, onItemSelected: (Int) -> Unit) { BottomAppBar { BottomNavigationItem( icon = { Icon(painterResource(R.drawable.music_symbol), contentDescription = null) }, selected = selectedScreen == 0, onClick = { onItemSelected(0) } ) BottomNavigationItem( icon = { Icon(painterResource(R.drawable.heart_empty), contentDescription = null) }, selected = selectedScreen == 1, onClick = { onItemSelected(1) } ) } } -> when user clicks to a category item it goes to artist detail screen composable . But i want this artist detail screen composable opens in the first screen composable so i can still be able to use my bottom bar , can you do it for me.
- Assistant
Here's an updated version of the
MainScreenandMusicCategoriesScreencomposable functions. They're now using nested navigation, so theArtistDetailScreenwill be a part of the first screen while still keeping the bottom bar.@Composable fun MainScreen() { val navController = rememberNavController() Scaffold( bottomBar = { BottomBar(selectedScreen = navController.currentBackStackEntry?.destination?.route, onItemSelected = { route -> navController.navigate(route) { navController.graph.startDestinationRoute?.let { popUpTo(it) { saveState = true } } launchSingleTop = true restoreState = true } }) }, content = { NavHost(navController, startDestination = "musicCategories") { composable("musicCategories") { MusicCategories(navController) } composable("secondScreen") { SecondScreen() } } } ) } @Composable fun MusicCategories(navController: NavController) { val categoryNavController = rememberNavController() Column { TopBar(title = "Music Categories") NavHost(categoryNavController, startDestination = "categories") { composable("categories") { val categoriesViewModel: CategoriesViewModel = viewModel() val categories by categoriesViewModel.categories.collectAsState() MusicCategoriesScreen( categories = categories, onCategorySelected = { category -> categoryNavController.navigate("artistsDetail/${category.id}") } ) } composable("artistsDetail/{categoryId}") { backStackEntry -> val categoryId = backStackEntry.arguments?.getString("categoryId")?.toIntOrNull() if (categoryId != null) { ArtistsDetailScreen(categoryId) } } } } // When the back button is pressed, pop the nested navController first. BackHandler(onBack = { if (categoryNavController.currentBackStackEntry?.destination?.route != "categories") { categoryNavController.popBackStack() } else { navController.navigate("secondScreen") } }) } @Composable fun BottomBar(selectedScreen: String?, onItemSelected: (String) -> Unit) { BottomAppBar { BottomNavigationItem( icon = { Icon(painterResource(R.drawable.music_symbol), contentDescription = null) }, selected = selectedScreen == "musicCategories", onClick = { onItemSelected("musicCategories") } ) BottomNavigationItem( icon = { Icon(painterResource(R.drawable.heart_empty), contentDescription = null) }, selected = selectedScreen == "secondScreen", onClick = { onItemSelected("secondScreen") } ) } }In this code, I've moved
MusicCategoriesinto a separate composable and addedrememberNavControllerto manage nested navigation for categories and artist detail screens.BackHandleris used to handle the back button press for nested navigation properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.