I assume you want to stop playing the song when you navigate away from the…
- User
I assume you want to stop playing the song when you navigate away from the AlbumDetailScreen. To do this, you can use the DisposableEffect function in your AlbumDetailScreen composable. It allows you to perform some action when the composable is disposed, which happens when you navigate away from the screen.
Here’s an updated version of the AlbumDetailScreen with the DisposableEffect: -> u were about to remake code for me but your connection lost. This is my AlbumDetailScreen, implement the disposableEffect please ->package com.example.musicapp.Interface
import TopBar import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.example.musicapp.Data.Song import com.example.musicapp.ViewModel.AlbumDetailsViewModel import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.navigation.NavController import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import coil.compose.rememberImagePainter import coil.transform.CircleCropTransformation import com.example.musicapp.ViewModel.PlayerViewModel import com.example.musicapp.R import androidx.lifecycle.ViewModelStoreOwner import com.example.musicapp.Data.Repository.DeezerRepository
@Composable fun AlbumDetailScreen(albumId: Int, navController: NavController, localViewModelStoreOwner: ViewModelStoreOwner ) { val context = LocalContext.current val deezerRepository = remember { DeezerRepository() } // memoize to avoid re-initialization val albumDetailsViewModel: AlbumDetailsViewModel = viewModel()
code// Replace the previous playerViewModel assignment with this code: val playerViewModel = remember { PlayerViewModel(context) } albumDetailsViewModel.fetchAlbumDetails(albumId) val albumDetails = albumDetailsViewModel.albumDetails.collectAsState().value albumDetails?.let { details -> Scaffold( topBar = { TopBar(title = details.title) }, content = { padding -> Column( modifier = Modifier.padding(padding) ) { LazyColumn { itemsIndexed(details.songs) { _, song -> // Use itemsIndexed instead of items AlbumDetailItem(song, navController, playerViewModel) } } } } ) } - Assistant