I wrote my app with Jetpack Compose and navigation component. Also using MVVM. because of Navigating with Compose, there is no fragments , I have just composable screen associate with view model. now I wondering where should I release my resources like Media Player? because media player is UI thing, my view model does not know anything about it.
CodePudding user response:
if I get right you have to create a funtion and use androidview to wrap the mediaplayer component, but if what you want is to load a media file you have to place it in res/raw and access throught context, to get the context in a compose function you have to use LocalContext.current
https://developer.android.com/jetpack/compose/interop/interop-apis?hl=en-419
CodePudding user response:
As long as you have a single activity, always override the onDestroy()
method of the activity lifecycle to dispose of everything when your app is killed (either by the user or by the system to save resources).
override fun onDestroy(){
mediaPlayer.release()
super.onDestroy()
}
Now, your ideology of "viewmodel knows nothing of the UI" is lethal. Please take the State in Compose codelab paying special attention to the part explaining state-hoisting.