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
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.