null from Clipboard

Viewed 461

I need to get copied data from clipboard. I use this code:

val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData: ClipData? = clipboardManager.primaryClip
clipData?.let { textView.text = clipData.getItemAt(0).text }

If I use this code inside onCreate() or onResume() callbacks, I always getting null from clipboard.

But if I call this code:

textView.post {
        val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clipData: ClipData? = clipboardManager.primaryClip
        clipData?.let { textView.text = clipData.getItemAt(0).text }
}

I get copied string.

So, I make conclusion, that Clipboard waits until all views are rendered.

Why clipboard needs to wait for rendering all views? Or maybe clipboard is waiting for something else

1 Answers
Related