0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

onGlobalFocusChangedにnullを食わされる

Last updated at Posted at 2019-11-23

AndroidTVとか、キー操作でフォーカスを移動するアプリを作る際に使いがちな
ViewTreeObserver.OnGlobalFocusChangeListener

唯一のPublic methidはdeveloper.android.comのreferenceによると以下のように定義されています
abstract fun onGlobalFocusChanged(oldFocus: View!, newFocus: View!): Unit

だけど実際に使ってみるとoldFocusにnullが入ってきたりします
https://developer.android.com/reference/kotlin/android/view/ViewTreeObserver.OnGlobalFocusChangeListener.html
には以下のような記載が
When the view tree transitions from touch mode to non-touch mode, oldFocus is null.

kotlinではAndroid StudioがView!で補完し、うっかりしていると補完されるままにNotNullとして実装してしまいCrashへといざなわれます
英語に堪能な仲間の助力を得てドキュメントのバグを報告したいと思います

追記
実際のところ、
When focus changes in non-touch mode (without transition from or to touch mode) either oldFocus or newFocus can be null.
について簡単にエミュレータで確認したところ
Activtyの初期フォーカスが入るところではoldFocusがnullになる、という比較的わかりやすい挙動の他
API17では常にnullが渡されるように見えます

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?