I have an activity with swiping tabs using ActionBar tabs, based on the android developer example.
Each tab displays a Fragment, and each Fragment (actually, a SherlockFragment) loads a different kind of remote api request via a custom AsyncTaskLoader.
The problem is that if you tap a tab to move 2 tabs/pages over while the fragment for the tab you are leaving (the old fragment) is loading a result, that result is delivered to the fragment for the tab you move to (the new fragment). In my case, this leads to a ClassCastException, since the expected results are of incompatible types.
In code, the gist of the situation is:
Loaders:
public class FooLoader extends AsyncTaskLoader<Foo>
public class BarLoader extends AsyncTaskLoader<Bar>
Fragments:
public class FooFragment extends Fragment implements LoaderManager.LoaderCallbacks<Foo> {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
public Loader<Foo> onCreateLoader(int id, Bundle args) { return new FooLoader(); }
...
}
public class BarFragment extends Fragment implements LoaderManager.LoaderCallbacks<Bar> {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
public Loader<Bar> onCreateLoader(int id, Bundle args) { return new BarLoader(); }
...
}
The tab management code is as in the the aforementioned example. There is a third tab in between the Foo and Bar tabs (call it Baz). When we skip from the Foo tab to the Bar tab by