I have a TextView that I fill with internal app links using ClickableSpans. I'm using Kotlin, but I'll take answers in Kotlin or Java.
val spannableString = SpannableString(myTextView.text)
val clickableSpan = object : ClickableSpan() {
override fun onClick(view: View) {
// handle click
}
}
spannableString.setSpan(clickableSpan, spanStartIndex, spanEndIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
myTextView.movementMethod = LinkMovementMethod.getInstance()
myTextView.setText(spannableString, TextView.BufferType.SPANNABLE)
When I toggle Dark Mode on the phone, all my links disappear! If I reload the view, the links are regenerated and work again, but until then the text goes back to being plain text. This happens switching from light to dark mode and vice versa.
I'm looking for a clean way to fix this problem, without reloading my activity if the user switches light/dark modes. Is there something automatic that could fix my problem? Maybe somehow saving the spans to a savedInstanceState bundle if that would work? Or is there a OnNightModeEnabled function I'm not aware of that I'll need to override?