I use Weakreferences for the callback in a asynctask. In the constructor of the asynctask i give a list with references. In my situation the list contains 3 references, 2 fragment references and 1 java class reference.
When i check the list which is recieved by the constructor of the asynctask the list contains the 3 (filled) references. I copy these to a local list (in the asynctask). When i check this list this has also the 3 (filled) references.
Then the asynctask is (doInBackground) is executed which don't touch the references. When i check the references with a breakpoint at the first line in the doInBackground method the 3th (java class) reference is null. The other 2 references are still filled.
Everything worked fine till tomorrow. I checked the differences in code but there are no big differences. I tryed to roll them back with no result. Has someone an explaination for this?
I created the references on this way:
WeakReference<e_Alerts> wr = new WeakReference<e_Alerts>(this);
callbackReferences.add(new WeakReference<>((e_Alerts)tab_AlertListOverviewFragment));
callbackReferences.add(new WeakReference<>((e_Alerts)tab_AlertMapsOverviewFragment));
And the list i used is a simple List<WeakReference<e_Alerts>> callbackReferences;
list.
--------------------------------------------------------Update--------------------------------------------------------
doInBackground code:
try {
//Downloads the alert XMLs from the internet and parses it to xmlAlerts
this.alerts = new XmlDownloader().DownloadAlerts(inputUrl);
// Filters the xml alerts so only the alerts where the enduser is interessed in will remain
this.alerts = filterAlerts(this.alerts);
// Converts the remaining xmlAlerts to Alerts;
this.result = new AlertConverter().Convert(this.alerts);
}catch (Exception e) {
Log.e("At_allAlerts",e.getMessage());
}
return null;
filterAlerts Method:
private List<Item> filterAlerts(List<Item> alerts) {
List<Item> filteredXmlAlerts = new ArrayList<>();
for (Item alert : alerts)
{
Location alertLocation = new Location("");
alertLocation.setLatitude(alert.getGeometries().get(0).getLocations().get(0).getLat());
alertLocation.setLongitude(alert.getGeometries().get(0).getLocations().get(0).getLng());
for(Area area : this.areas)
{
if (area.IsOrganization() && alert.getCountryCode().toLowerCase().equals(area.getOrganizationcode().toLowerCase())){
filteredXmlAlerts.add(alert);
break;
}
else if(!area.IsOrganization() && isAlertInRegion(alertLocation, area)) {
filteredXmlAlerts.add(alert);
break;
}
}
}
return filteredXmlAlerts;
}
The XmlDownloader: Downloads an xml feed an parses the xml to objects with a library
The AlertConverter: converts the xml object to the object i use in my app
Both classes can work without the asynctask class and don't use the references.