Android AsyncTasks How to Check If activity is still running

前端 未结 8 1488
时光说笑
时光说笑 2021-02-07 04:25

I have used AsyncTasks with my application, in order to lazy download and update the UI.

For now my AsyncTasks updates the UI real simply:

8条回答
  •  长发绾君心
    2021-02-07 05:18

    Even though, I have never faced this scenario; I will try to answer your question.

    In your case you will need to validate the Context passed to AsyncTask.

    You can perform validation

    if(null!=mContext)  //Activity still exist!!
    {
      gender.setText(values[0]);
    }
    else //Activity is destroyed
    {
       //Take appropriate action!!
    }  
    

    The advantage will be, if the activity is destroyed by the time you reach this statement, your Context will automatically become null and you can handle the scenario.

提交回复
热议问题