5

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: enter image description here

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

Jorgesys
  • 114,263
  • 22
  • 306
  • 247
BigBen3216
  • 838
  • 1
  • 8
  • 23
  • As it comes from your debugger's screens, the `adapterList` is an instance of inner class of your `Fragment`. Could it be the issue? – ernazm Nov 22 '12 at 18:21

1 Answers1

1

...does anyone knows why the call back method is keeping the reference to the adapter and listview that where created before the orientation change?

(This answer comes without knowing your callback implementation or how your AsyncTask looks) Why the callback shouldn't keep the reference to the old fragment? You set in that Button's listener the current Fragment instance and then do the device rotation while the task runs. You don't have any code in your Fragment that, as it is reconstructed after the configuration changes, will update the callback instance in your AsyncTask to point to the new fragment. Depending on how you use that Fragment you could use the Fragment.setRetainInstance() method which will prevent the Fragment to be destroyed so your task will have the same callback instance even after the rotation. Also have a look at an answer from one of the Android engineers regarding this particular problem.

Community
  • 1
  • 1
user
  • 85,380
  • 17
  • 189
  • 186