Callback method with reference to the old fragment

I have a fragment activity containing a fragment, The fragment starts Asynctask, which loads some data, I applied a callback method in my fragment, which updates some values โ€‹โ€‹in the adapter and the list. I have the following problem: 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 AsyncTask is running, the doInBackground () method continues to work, and then after starting it, it starts the listener and runs the callback method in my fragment, this method has old links of my adapter and my View list: enter image description here

The fragment and its contents are recreated when the orientation changes and this is correct, but does anyone know why the callback method saves the link to the adapter and the list created before the orientation change?

EDIT:

I have a button that executes asynctask as follows:

 asyncRefresh = new PullRefreshTask(taskContext, mContext, secciones); asyncRefresh.setUpdatePull2RefreshListener(this); asyncRefresh.execute(); 

If the user clicks the button, asyncTask will set the old fragment as a listener, and when the orientation changes during the synthesis, I thought that the activated callback method was the only one from the method of the newly created fragment, but I'm not finite.

EDIT 2:

I solved my problem since I said in my first edit that the callback method is being called for the old fragment. so I did to save my async in a variable in another class called "Information", and in the "Create View" mode 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); } 

So I set a new link to my fragment in

setUpdatePull2RefreshListener ()

running synthetics method

+4
source share
1 answer

... does anyone know why the callback method keeps a link to the adapter and the list that were created before the orientation change?

(This answer comes without knowing your callback implementation or what your AsyncTask looks like) Why shouldn't the callback contain a link to the old snippet? You install a current Fragment instance in this Button listener, and then rotate the device while the task is running. You do not have code in Fragment that, as it will be restored after configuration changes, updates the callback instance in your AsyncTask to point to the new fragment. Depending on how you use this Fragment , you can use the Fragment.setRetainInstance() method, which will prevent the Fragment from being destroyed so that your task will have the same callback instance even after the rotation. Also see the answer from one of the Android engineers regarding this particular problem.

+1
source

Source: https://habr.com/ru/post/1447795/


All Articles