0

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?

Cullub
  • 2,901
  • 3
  • 30
  • 47
  • In which method have you written the above code? – Nataraj KR Mar 21 '20 at 12:06
  • It's in its own method called `processContent(text: CharSequence): SpannableString`. I simplified it for this post. The method is in a Fragment, not an Activity class if that's useful at all. – Cullub Mar 21 '20 at 14:01
  • where is this `processContent` method called: I mean which lifecycle method of fragment. – Nataraj KR Mar 21 '20 at 14:36
  • It's actually called as a callback for an async call. That call is executed from `onCreateView` after the layout is inflated. – Cullub Mar 21 '20 at 15:17

0 Answers0