0
  • 最佳答案

    你可以自定义一个 `ViewModelProvider.Factory` ,在创建的时候通过 `ViewModelProvider.Factory` 传入你需要的参数,就像官方示例的这样

    https://developer.android.com/codelabs/android-room-with-a-view-kotlin#9

    1204736502274318336  评论     打赏       A lonely cat
    • 先这样搞了

      class BeatBoxViewModel : ViewModel() {
          var beatBox: BeatBox? = null
      
          fun initializeBeatBox(assets: AssetManager) {
              if (beatBox == null) {
                  beatBox = BeatBox(assets)
              }
              
          }
      
          override fun onCleared() {
              super.onCleared()
              beatBox?.release()
          }
      }
      
      1265625074946338816  评论     打赏       L0NGING
      相关问题
      资质平平 · Android
      2025-02-20 09:14 92 100
      尖沙咀-段坤 · 项目
      2025-01-06 23:39 18 2
      雅澤yaduo · 领券联盟
      2025-01-03 20:39 37 50
      呆瓜小董 · webview
      2024-11-30 18:02 68 20
      阿肥 · 鸿蒙next
      2024-10-25 18:07 35 100
      尖沙咀-段坤 · 安卓
      2024-09-11 11:03 31 2
      YanLQ · AOSP
      2024-08-10 11:57 35 100
      断点 · vue
      2024-08-08 10:05 79 30