0

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");
        }
    }
reidisaki
  • 1,525
  • 16
  • 29
  • Post the code of the fragment. – user Feb 05 '15 at 06:45
  • updated with fragment code. – reidisaki Feb 05 '15 at 17:12
  • *They are their own instances of the same type of class.* You say this but for some reasons you keep a static reference to the fragment. – user Feb 05 '15 at 17:46
  • I'm just following what I read on developer.android.com.. if I don't use addTab(t,true) and just use addTab(t); I click through each tab, everything renders normally and it all works as expected. It's just when I have addTab(t,true) it will loop over onTabSelected() and the fragments don't work. I guess what I'm trying to say is that why does this not work when addTab(t,true) is in a loop, vs. physically tapping each tab to call onTabSelected()? – reidisaki Feb 05 '15 at 17:58
  • As I said, we do you made f, showName and tabName static in that fragment? – user Feb 05 '15 at 18:34
  • ok you're right, i've eliminated all static references, but now how do I add the fragment once it's been instantiated and populated to the actionBar tab. I was following something like this http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – reidisaki Feb 05 '15 at 18:35
  • That is your job. There's a tutorial on the android site http://developer.android.com/guide/topics/ui/actionbar.html#Tabs – user Feb 05 '15 at 18:58

0 Answers0