2023年12月30日 星期六

【Android】 在 Views 中使用 Compose (3) : 在片段的 LifecycleOwner 遭到刪除時銷毀 Composition


實例

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentBlank2Binding.inflate(inflater, container, false)
        val view = binding.root
        binding.apply {
            composeView.apply {
                setContent {
                    // You're in Compose world!
                    MaterialTheme {
                        PlantDetailDescription(viewModel)
                    }
                }

                // Dispose the Composition when the view's LifecycleOwner
                // is destroyed
                // 當視圖的 LifecycleOwner 被銷毀時處置 Composition
                setViewCompositionStrategy(
                    ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
                )
            }
        }
        return view
    }


參考資料



0 comments:

張貼留言