How to return an object from the asynctask to the main class in android

前端 未结 3 1448
夕颜
夕颜 2021-01-24 21:29

I want to return the document to my main class but even using a global variable dosen\'t work it\'s because the asynctask didn\'t finish the job I think is there a solution to g

3条回答
  •  终归单人心
    2021-01-24 21:29

    OnPostExecute is where you receive the Document when it finishes downloading in the main thread, that is the place where you want to return tour item.

    It occurs to me that you can implement a constructor in the asynctask, something like

    private class RequestTask extends AsyncTask {
    
      private MainClass myClass
    
      public RequestTask(MainClass myClass){
         this.myClass = myClass
      }
    
    ...
    
      protected void onPostExecute(Document doc) {
        super.onPostExecute(doc);
        myClass.myMethod(doc);
      }
    ...
    }
    

    That way you can receive the Document in your main Class

    Hope it helps

    Greetings

提交回复
热议问题