androidkotlinandroid-jetpack-composeclipboardclipboardmanager

How to resolve deprecated ClipboardManager in Jetpack Compose?


The ClipboardManager interface was deprecated. What should I change?


Solution

  • The ClipboardManager interface was deprecated in favor of Clipboard interface.

    1. Change all import androidx.compose.ui.platform.LocalClipboardManager to import androidx.compose.ui.platform.LocalClipboard and LocalClipboardManager to LocalClipboard

    2. Make the caller function suspend or use rememberCoroutineScope().launch { ... }

    3. If you were using function setText(AnnotatedString(text)), then you can replace it with setClipEntry(ClipEntry(ClipData.newPlainText(text, text)))

    According to the Jetpack Compose tests, the replacement for hasText() is getClipEntry()?.clipData?.description?.hasMimeType("text/*")