I have a fragment activity that contains a Fragment , the Fragment starts an Asynctask that downloads some data, I have implemented a callback method in my Fragment that updates some values in an adapter and a listview. The problem I have is the following: This is my onCreateView method(important part):
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
list=(PullToRefreshListView)v1.findViewById(R.id.listapull);
adapterList=new ListViewAdapter(secciones, mContext);
}
when I rotate the device while the AsyncTask is running, the doInBackground() method keeps running, then on post execute it triggers the listener and starts the callback method in my fragment, this method has the old references of my adapter and my list view:
The fragment and its content are recreated when an orientation change happens and that is correct, but does anyone knows why the call back method is keeping the reference to the adapter and listview that where created before the orientation change?
EDIT:
I have a button that executes the asynctask like this:
asyncRefresh = new PullRefreshTask(taskContext, mContext, secciones);
asyncRefresh.setUpdatePull2RefreshListener(this);
asyncRefresh.execute();
If the user press the button the asyncTask will set old Fragment as the listener and when an orientation change occurs while the asynctask is running, I thought the activated callback method was the one from the newly created fragment method but I'm not sure anymore.
EDIT 2:
I have resolved my problem, as i said in my first edit the call back method was being called for the old fragment . so what I did was to save my asynctask in a variable in another class named "Info" and in on create view i did this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
list=(PullToRefreshListView)v1.findViewById(R.id.listapull);
adapterList=new ListViewAdapter(secciones, mContext);
PullRefreshTasktask task = Info.getAsyncTask();
asyncRefresh = task;
asyncRefresh.setUpdatePull2RefreshListener(this);
}
This way I set the new reference of my fragment in my
setUpdatePull2RefreshListener()
method of the running asynctask