So the issue I'm having is that I loop over a list of tabs, and call
actionBar.addTab(t,true) //which will force the app to basically call onTabSelected() for each tab
each tab then instantiates a fragment, and inside the fragment on onCreateView() I fire an asynctask per fragment. Each Fragment is the same class subclassing Fragment. They are their own instances of the same type of class.
The problem now is that if I have 3 tabs, say "Recent, Trending, Editor's Picks" the flow of execution is normal until I get to onPostExecute.. why does it only call "Editor's Picks" onPostExecute 3 times. It's like asynctask's onPostExecute got overwritten by the last tab. The order is Recent, Trending, Editor's picks at the end. Whatever is called last is what gets rendered.
here is my logcat:
I/reid ( 514): onPreExeucte:Recent
I/reid ( 514): DoInBackground:Recent
I/reid ( 514): onCreateView: Recent
I/reid ( 514): onPreExeucte:Trending
I/reid ( 514): onCreateView: Trending
I/reid ( 514): DoInBackground:Trending
I/reid ( 514): onPreExeucte:Editor's Picks
I/reid ( 514): DoInBackground:Editor's Picks
I/reid ( 514): onCreateView: Editor's Picks
I/reid ( 514): onPostExecute:Editor's Picks
I/reid ( 514): onPostExecute:Editor's Picks
I/reid ( 514): onPostExecute:Editor's Picks
Here is the code of the EpisodeTileFragment
public class EpisodeTileFragment extends Fragment {
public List<Episode> _episodes = new ArrayList<Episode>();
int offset = 0;
ListView list;
ListEpisodesAdapter adapter;
static String showName, tabName;
OnEpisodesLoadedListener mCallback;
static EpisodeTileFragment f;
View v;
public interface OnEpisodesLoadedListener {
public void isEpisodesLoaded(View v);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.episode_tiles, container, false);
v.setVisibility(View.GONE);
list = (ListView)v;
_episodes.clear();
tabName = getArguments().getString("show", "not set");
new LoadEpisodesTask().execute(tabName);
Log.i("Reid","arguments: episodeTileFragment: " + getArguments().getString("show", "not set"));
return v;
}
public static EpisodeTileFragment newInstance(String text) {
f = new EpisodeTileFragment();
Bundle b = new Bundle();
b.putString("title", text);
f.setArguments(b);
return f;
}
private final class LoadEpisodesTask extends AsyncTaskEx<String, Episode, IOperationResult> {
@Override
protected void onPreExecute() {
Log.i("reid","onPreExeucte:" + tabName);
}
@Override
protected IOperationResult doInBackground(String... s) {
IPaginatedProgressListener<Episode> listener = new IPaginatedProgressListener<Episode>() {
@Override
public void publishProgress(Episode progress) {
LoadEpisodesTask.this.publishProgress(progress);
}
@Override
public void setTotal(int total) {
}
};
offset = _episodes.size();
String GroupingType = "latest";
if(s[0].equalsIgnoreCase(getResources().getString(R.string.title_section3))){
GroupingType = "trending";
}
Log.i("reid","DoInBackground:" + tabName);
return API.get().getEpisodes(listener, offset, 25, GroupingType);
// return API.get().getEpisodesByShow(listener, show, offset, 25);
}
@Override
protected void onProgressUpdate(Episode... values) {
_episodes.add(values[0]);
}
@Override
protected void onPostExecute(IOperationResult result) {
if(_episodes != null) {
adapter = new ListEpisodesAdapter(getActivity(), R.layout.episode_tile, _episodes, false);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
mCallback.isEpisodesLoaded(v);
Log.i("reid","onPostExecute:" + tabName);
}
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnEpisodesLoadedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnEpisodesLoadedListener");
}
}