Android Refreshing Fragment View after AsyncTask

后端 未结 1 945
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 11:07

I am trying to update list fragment when an AsyncTask in my activity completes but I am not sure if I am doing something wrong. Currently, I have a button that

相关标签:
1条回答
  • 2020-12-18 11:45

    First replacing a fragment is nothing like refreshing it. Yes, it reloads every view in it and data as well. But you have to relate it with your new data. So i would recomment you to create a refresh method in your fragment and send this method your refreshed data, then notify your adapter as dataSetChanged.

    To achieve this, you're gonna need to reach your currently attached fragment and call its refresh method. You can use findFragmentByTag to reach it out.

    Edit: To clarify little bit more

    When your AsyncTask is done, you should do something like this onPostExecute method:

    ResultFragment resultFrag = (ResultFragment) getSupportFragmentManager()
                                     .findFragmentByTag("FragToRefresh");
    if (resultFrag != null) {
        resultFrag.refreshData(refreshedArray);
    }
    

    And in your ResultFragment you need to have refreshData method, which is something like this:

    public void refreshData(ArrayList<YourObject> data) {
       yourArray = new ArrayList<YourObject>(data);
       yourAdapter.notifyDataSetChanged();
    }
    

    And your list will be refreshed whenever your task is done.

    0 讨论(0)
提交回复
热议问题