如果在一个Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,如果Activity被结束了,则我们需要写如下代码作为防守:

@Override public void onPostExecute(Result r) {
    if (getActivity() == null) {
        return;
    }
    // ...
}

Activity被终止之后,如果继续使用其中的Context等,除了无辜的浪费CPU,电池,网络等资源,有可能还会导致程序crash,所以,我们需要处理这种一场情况。

相关文章:

  • 2021-06-08
  • 2021-08-04
  • 2021-09-17
  • 2022-01-15
  • 2021-12-15
猜你喜欢
  • 2021-07-31
  • 2022-12-23
  • 2021-05-05
  • 2022-12-23
  • 2023-03-09
  • 2021-12-01
相关资源
相似解决方案