简体   繁体   中英

Timing AsyncTask onPostExecute method in two Fragments to finish at the same time in android

I have two Android Fragments (Fragment A and Fragment B) both Fragment A and Fragment B executes an AsyncTask that subsequently populates a listView by calling on an API, which works fine. However the listView is Fragment A takes longer to populate than the listView in Fragment B and I need the Fragment B listView and Fragment A listview to be in sync in how long it takes them to completed the AsyncTask onPostExecute method. How do you suggest that I go about accomplishing this.

You can save the results of the Asynctasks after onPostExecute() in variables and call a function to check whether both variable are initialized, and if yes, you can then do your operation of setting the list to the list views.

Like,


    List<String> fragAList,fragBList;

    public void saveFirstList(List<String> data){
        fragAList=data;
        updateLists();
    }

    public void saveSecondList(List<String> data){
        fragBList=data;
        updateLists();
    }

    public void updateLists(){
        if(fragAList!=null && fragBList!=null){
            setDataToListViews(fragAList,fragBList);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM