onAttachToWindow & onDetachFromWindow

  • 取消网络请求的时机
  • 参考文章

取消网络请求的时机

看代码的时候,发现某个控件在内部做了请求网络的操作。问题来了,如果控件移除屏幕或页面被销毁,则需要处理下未完成的网络请求,将其取消掉。取消的时机如何选择?由于是公共控件,希望能在控件内部完成取消操作,候选时机包括:

  • onAttachToWindow()
  • onDetachFromWindow()
  • onFinishTemporaryDetach()
  • onStartTemporaryDetach()

其中 onDetachFromWindow() 在页面退出的时候都会被调用。而 onStartTemporaryDetach() 则有所不同。

onAttachToWindow & onDetachFromWindow_第1张图片

ListView 在 7.0 机器上滚动列表:

06-28 17:32:34.914 24928-24928/? D/MyTextView: onAttachedToWindow: 0
06-28 17:32:34.918 24928-24928/? D/MyTextView: onAttachedToWindow: 1
06-28 17:32:34.920 24928-24928/? D/MyTextView: onAttachedToWindow: 2
06-28 17:32:34.923 24928-24928/? D/MyTextView: onAttachedToWindow: 3
06-28 17:32:34.926 24928-24928/? D/MyTextView: onAttachedToWindow: 4
06-28 17:32:34.928 24928-24928/? D/MyTextView: onAttachedToWindow: 5
06-28 17:32:34.930 24928-24928/? D/MyTextView: onAttachedToWindow: 6
06-28 17:32:34.932 24928-24928/? D/MyTextView: onAttachedToWindow: 7
06-28 17:32:34.933 24928-24928/? D/MyTextView: onAttachedToWindow: 8
06-28 17:32:34.935 24928-24928/? D/MyTextView: onAttachedToWindow: 9
06-28 17:32:34.937 24928-24928/? D/MyTextView: onAttachedToWindow: 10
06-28 17:32:34.940 24928-24928/? D/MyTextView: onAttachedToWindow: 11
06-28 17:32:34.941 24928-24928/? D/MyTextView: onAttachedToWindow: 12
06-28 17:32:34.943 24928-24928/? D/MyTextView: onAttachedToWindow: 13
06-28 17:32:34.945 24928-24928/? D/MyTextView: onAttachedToWindow: 14
06-28 17:32:34.947 24928-24928/? D/MyTextView: onAttachedToWindow: 15
06-28 17:32:34.949 24928-24928/? D/MyTextView: onAttachedToWindow: 16
06-28 17:32:34.951 24928-24928/? D/MyTextView: onAttachedToWindow: 17
06-28 17:32:34.953 24928-24928/? D/MyTextView: onAttachedToWindow: 18
06-28 17:32:34.955 24928-24928/? D/MyTextView: onAttachedToWindow: 19
06-28 17:32:34.957 24928-24928/? D/MyTextView: onAttachedToWindow: 20
06-28 17:32:34.959 24928-24928/? D/MyTextView: onAttachedToWindow: 21
06-28 17:32:34.961 24928-24928/? D/MyTextView: onAttachedToWindow: 22
06-28 17:32:34.964 24928-24928/? D/MyTextView: onAttachedToWindow: 23
06-28 17:32:34.967 24928-24928/? D/MyTextView: onAttachedToWindow: 24
06-28 17:32:34.969 24928-24928/? D/MyTextView: onAttachedToWindow: 25
06-28 17:32:34.971 24928-24928/? D/MyTextView: onAttachedToWindow: 26
06-28 17:32:34.973 24928-24928/? D/MyTextView: onAttachedToWindow: 27
06-28 17:32:34.975 24928-24928/? D/MyTextView: onAttachedToWindow: 28
06-28 17:32:40.406 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 0
06-28 17:32:40.406 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 1
06-28 17:32:40.406 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 2
06-28 17:32:40.406 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 3
06-28 17:32:40.407 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 29
06-28 17:32:40.408 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 30
06-28 17:32:40.408 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 31
06-28 17:32:40.409 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 32
06-28 17:32:40.422 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 4
06-28 17:32:40.422 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 5
06-28 17:32:40.423 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 33
06-28 17:32:40.424 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 34
06-28 17:32:40.438 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 6
06-28 17:32:40.439 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 7
06-28 17:32:40.439 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 35
06-28 17:32:40.440 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 36
06-28 17:32:40.472 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 8
06-28 17:32:40.472 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 9
06-28 17:32:40.472 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 37
06-28 17:32:40.473 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 38
06-28 17:32:40.489 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 10
06-28 17:32:40.489 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 39
06-28 17:32:40.522 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 11
06-28 17:32:40.522 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 40
06-28 17:32:40.555 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onStartTemporaryDetach: 12
06-28 17:32:40.556 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onFinishTemporaryDetach: 41
06-28 17:32:40.657 24928-24928/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 42

ListView,在 8.0 上滚动列表:

06-28 17:37:29.332 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 0
06-28 17:37:29.333 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 1
06-28 17:37:29.335 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 2
06-28 17:37:29.336 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 3
06-28 17:37:29.338 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 4
06-28 17:37:29.339 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 5
06-28 17:37:29.340 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 6
06-28 17:37:29.341 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 7
06-28 17:37:29.342 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 8
06-28 17:37:29.343 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 9
06-28 17:37:29.344 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 10
06-28 17:37:29.346 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 11
06-28 17:37:29.347 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 12
06-28 17:37:29.348 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 13
06-28 17:37:29.350 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 14
06-28 17:37:29.352 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 15
06-28 17:37:29.353 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 16
06-28 17:37:29.355 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 17
06-28 17:37:29.356 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 18
06-28 17:37:29.358 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 19
06-28 17:37:29.358 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 20
06-28 17:37:29.359 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 21
06-28 17:37:29.360 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 22
06-28 17:37:29.361 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 23
06-28 17:37:29.362 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 24
06-28 17:37:29.363 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 25
06-28 17:37:29.365 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 26
06-28 17:37:29.366 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 27
06-28 17:37:30.970 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 28
06-28 17:37:30.985 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 29
06-28 17:37:31.000 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 30
06-28 17:37:31.016 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 31
06-28 17:37:31.048 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 32
06-28 17:37:31.065 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 33
06-28 17:37:31.082 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 34
06-28 17:37:31.083 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 35
06-28 17:37:31.100 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 36
06-28 17:37:31.100 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 37
06-28 17:37:31.115 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 38
06-28 17:37:31.131 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 39
06-28 17:37:31.131 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 40
06-28 17:37:31.147 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 41
06-28 17:37:31.148 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 42
06-28 17:37:31.164 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 43
06-28 17:37:31.181 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 44
06-28 17:37:31.198 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 45
06-28 17:37:31.199 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 46
06-28 17:37:31.215 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 47
06-28 17:37:31.231 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 48
06-28 17:37:31.248 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 49
06-28 17:37:31.264 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 50
06-28 17:37:31.265 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 51
06-28 17:37:31.281 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 52
06-28 17:37:31.298 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 53
06-28 17:37:31.332 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 54
06-28 17:37:31.349 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 55
06-28 17:37:31.366 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 56
06-28 17:37:31.382 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 57
06-28 17:37:31.416 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 58
06-28 17:37:31.432 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 59
06-28 17:37:31.466 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 60
06-28 17:37:31.499 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 61
06-28 17:37:31.532 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 62
06-28 17:37:31.583 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 63
06-28 17:37:31.632 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 64
06-28 17:37:31.699 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 65
06-28 17:37:31.799 31517-31517/com.tango4fun.listviewdemo D/MyTextView: onAttachedToWindow: 66

参考文章

  • Android中判断子View从ListView中移除

你可能感兴趣的:(Android,UI,开发)