Repository: android/compose-samples
File: Jetchat/app/src/main/java/com/example/compose/jetchat/profile/ProfileFragment.kt (Kotlin)

Code before:
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.example.compose.jetchat.MainViewModel
import com.example.compose.jetchat.theme.JetchatTheme

class ProfileFragment : Fragment() {

    private val viewModel: ProfileViewModel by viewModels()
    private val activityViewModel: MainViewModel by activityViewModels()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Consider using safe args plugin
        val userId = arguments?.getString("userId")
        viewModel.setUserId(userId)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        return ComposeView(context = requireContext()).apply {
            setContent {
                viewModel.userData.collectAsState().value.let { userData: ProfileScreenState? ->
                    JetchatTheme {
                        if (userData == null) {
                            ProfileError()
                        } else {
                            ProfileScreen(
                                userData = userData,
                                onNavIconPressed = {
                                    activityViewModel.openDrawer()
                                }
                            )
                        }
                    }
                }
            }
        }
    }
}


Review comment (suggestion):
Not sure if we should be avoiding using `collectAsState` in this way. This will update (and recompose?) even if the view is in the background.

Code after:
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.example.compose.jetchat.MainViewModel
import com.example.compose.jetchat.theme.JetchatTheme

class ProfileFragment : Fragment() {

    private val viewModel: ProfileViewModel by viewModels()
    private val activityViewModel: MainViewModel by activityViewModels()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Consider using safe args plugin
        val userId = arguments?.getString("userId")
        viewModel.setUserId(userId)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        return ComposeView(context = requireContext()).apply {
            setContent {
                viewModel.userData.observeAsState().value.let { userData ->
                    JetchatTheme {
                        if (userData == null) {
                            ProfileError()
                        } else {
                            ProfileScreen(
                                userData = userData,
                                onNavIconPressed = {
                                    activityViewModel.openDrawer()
                                }
                            )
                        }
                    }
                }
            }
        }
    }
}