title: onCreateView not called with Fragment in ViewPager
body: <p>I am using <code>ViewPager</code> from the ACL to show a couple of <code>Fragment</code>s. At first, these Fragments where all <code>ListFragment</code>s as they contains a list. I'm trying to mimic a 2 column table, so each element in the list contains another list of two objects. I want each cell to be long-clickable (not the entire list item), therefore I implemented my own ColumnLayout (perhaps it should be called CellLayout). Each list item contains two ColumnLayouts which are long-clickable and implements <code>getContextMenuInfo()</code> to return information about which entity was long-clicked. This method looks up the ViewPager, asks for the current fragment and then calls the fragment to return the entity id from the view that was long-clicked. In order to get the correct row the fragment needs to do:</p>
  
  <pre><code>getListView().getPositionForView(v)
  </code></pre>
  
  <p>And here's where the problem starts. Sometimes <code>getListView()</code> throws <code>IllegalStateException</code> saying "Content view not yet created". I.e. <code>onCreateView(...)</code> hasn't been called. This only happens sometimes when the app is resumed. Today I managed to re-produce the problem by  enabling the "Don't keep activities" option in Settings > Developer options on my Galaxy Nexus. I start the app, press Home and then re-open the app from the recent apps menu and now, if I long-click any of the cells in my listview this exception is thrown. By some debug outputs I managed to verify that onCreateView has never been called. Which is kind of strange because the entire ViewPager with its Fragment and its ListView and its cell items are all drawn!</p>
  
  <p>I was told on #android-dev that <code>ListFragment</code> in ACL does not support custom layouts so I re-implemented my <code>android.support.v4.app.Fragment</code> without using <code>ListFragment</code> but that didn't help.</p>
  
  <p><strong>Short summary:</strong> I have a custom layout which handles the event of long-pressing the layout for the purpose of creating a <code>MenuInfo</code> object containing information about entitiy in the cell pressed. In order to find the entity contained in the layout, <code>listview.getPositionForView(View v)</code> is eventually called, sometimes causing an <code>IllegalStateException</code>.</p>
  
  <p>Here my custom layout (perhaps there's an easier way to get hold of the <code>ViewPager</code> than digging in the view hierarchy):</p>
  
  <pre><code>package org.remotestick;
  
  import android.content.Context;
  import android.support.v4.view.ViewPager;
  import android.util.AttributeSet;
  import android.view.ContextMenu.ContextMenuInfo;
  import android.view.View;
  import android.widget.LinearLayout;
  
  public class ColumnLayout extends LinearLayout {
  
      public ColumnLayout(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public ColumnLayout(Context context) {
          super(context);
      }
  
      @Override
      protected ContextMenuInfo getContextMenuInfo() {
          int itemTypeId = getChildAt(0).getId();
          int positionTypeId = getId();
          View currentView = this;
          ViewPager viewPager = null;
          while(currentView.getParent() != null) {
              currentView = (View) currentView.getParent();
              if(currentView.getId() == R.id.pager) {
                  viewPager = (ViewPager) currentView;
                  break;
              } else if (currentView.getId() == R.id.rootLayout)
                  break;
          }
          if(viewPager == null)
              return null;
  
          WorkspaceAdapter adapter = (WorkspaceAdapter) viewPager.getAdapter();
          Pair&lt;Integer, Integer&gt; itemIdAndListPosition = adapter.getItemIdFromWorkspace(viewPager.getCurrentItem(), this, itemTypeId, (positionTypeId == R.id.leftColumn));
          if(itemIdAndListPosition == null)
              return null;
          else
              return new MatrixAdaptableContextMenuInfo(itemTypeId, itemIdAndListPosition.first, itemIdAndListPosition.second);
      }
  
      public static class MatrixAdaptableContextMenuInfo implements ContextMenuInfo {
  
          public final int itemTypeId;
          public final Integer itemId;
          public final Integer positionInList;
  
          public MatrixAdaptableContextMenuInfo(int itemTypeId, Integer itemId, Integer positionInList) {
              this.itemTypeId = itemTypeId;
              this.itemId = itemId;
              this.positionInList = positionInList;
          }
  
      }
  }
  </code></pre>
  
  <p>And here's (a snippet of) the <code>Fragment</code>:</p>
  
  <pre><code>public class EntityTableFragment extends Fragment implements TelldusEntityChangeListener {
  
      protected static final String ARG_TITLE = "title";
  
      protected MatrixAdapter mAdapter;
      protected ProgressViewer mProgressViewer;
      protected MatrixFilter mFilter;
      protected Handler mHandler = new Handler();
      protected RemoteStickData db;
  
      public boolean onAttach = false;
      public boolean onCreateView = false;
  
      private ListView listView;
  
      public static EntityTableFragment newInstance(String title) {
          EntityTableFragment f = new EntityTableFragment();
  
          Bundle args = new Bundle();
          args.putString(ARG_TITLE, title);
          f.setArguments(args);
          return f;
      }
  
      @Override
      public void onAttach(SupportActivity activity) {
          super.onAttach(activity);
          onAttach = true;
              try {
              mProgressViewer = (ProgressViewer) activity;
              mAdapter = new MatrixAdapter(getActivity(), mProgressViewer, new ArrayList&lt;List&lt;MatrixEntity&gt;&gt;(), null);
              TelldusLiveService.addListener(this);
          } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + " must implement ProgressViewer");
          }
          db = new RemoteStickData(getActivity());
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          TelldusLiveService.removeListener(this);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          listView.setAdapter(mAdapter);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.list, null);
          listView = (ListView) view.findViewById(R.id.list);
          return view;
      }
  
      @Override
      public String toString() {
          return getArguments().getString(ARG_TITLE);
      }
  
      public Pair&lt;Integer, Integer&gt; getIdAndPositionForView(View v, boolean isLeft) {
          if(listView == null)
              throw new IllegalStateException("Listview not yet created");
          int positionForView = listView.getPositionForView(v);
          if(isLeft)
              return new Pair&lt;Integer, Integer&gt;(mAdapter.getItem(positionForView).get(0).getId(), positionForView);
          else
              return new Pair&lt;Integer, Integer&gt;(mAdapter.getItem(positionForView).get(1).getId(), positionForView);
      }
  </code></pre>
  
  <p>Isn't it rather strange that, when resuming the app (if the activity was destroyed), the ViewPager with its Fragments and its ListView and its custom layouts are all drawn. Yet I get <code>IllegalStateException</code> saying the view hasn't been created if I long-press any of the cells in the listview?</p>
  
  <p>edit/
  Actually, a <code>Log.d(Constants.TAG, "onCreateView!!!!");</code> output in <code>onCreateView</code> shows that the methods is invoked two times the first time I start the app (one for each Fragment in the ViewPager) but then it's never called again when the app is resumed!? Is that a bug or what might I be doing wrong?</p>