How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

前端 未结 17 2198
鱼传尺愫
鱼传尺愫 2020-11-21 04:50

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute(

17条回答
  •  难免孤独
    2020-11-21 05:18

    Why do people make it so hard.

    This should be sufficient.

    Do not implement the onPostExecute on the async task, rather implement it on the Activity:

    public class MainActivity extends Activity 
    {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        //execute the async task 
        MyAsyncTask task = new MyAsyncTask(){
                protected void onPostExecute(String result) {
                    //Do your thing
                }       
    
        }
    
        task.execute("Param");
    
    }
    
    
    }
    

提交回复
热议问题