title: ListView's performItemClick() causes IllegalStateException when called inside onLoadFinished()
body: <p>I am implementing this <a href="http://i.stack.imgur.com/Z166s.png" rel="nofollow">Two Pane UI</a>. The list view on the left pane is populated by a loader. When a list item is clicked then right pane displays details about that item.</p>
  
  <p>I want to set the first item in the left pane to be selected by default and its details to be shown in the right pane. To do so, I have tried to call <code>performItemClick()</code> on my list like this:</p>
  
  <pre><code>listView.performItemClick(listView,
                            ListView.SCROLLBAR_POSITION_DEFAULT,
                            listView.getItemIdAtPosition(ListView.SCROLLBAR_POSITION_DEFAULT));
  </code></pre>
  
  <p>I'm doing this in the <code>onLoadFinished()</code> method of my loader because that is the point where I can be reasonably sure that the list view has been populated. This method runs in the UI thread, so I wasn't expecting any hiccups. However I get a <code>java.lang.IllegalStateException</code> error. </p>
  
  <p>I'd like to know why performing a click action in <code>onLoadFinished()</code> causes the exception whereas other calls on the list view like <code>smoothScrollToPosition()</code> get completed.</p>
  
  <p>From other SO posts I can see that it can be done using a <code>Handler()</code>. But it's not clear to me why scrolling the list can be done by a direct call but clicking an item requires posting to the message queue via a <code>Handler()</code>. I am fairly new to Android and there are gaps in my understanding of its architecture.</p>
  
  <p>Here's the full code that fails:</p>
  
  <pre><code>public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
      //Update the adapter
      mForecastAdapter.swapCursor(data);
  
      //If there is some item in the list that was selected before then scroll to it
      if(lastSelectedIndex != ListView.INVALID_POSITION) {
          //Restore to last scrolled position
          // Get a reference to the ListView, and attach this adapter to it.
          ListView listView = (ListView) getActivity().findViewById(R.id.listview_forecast);
  
          listView.smoothScrollToPosition(lastSelectedIndex);
      }
  
      //select the first element if two pane is supported and last state doesn't exist
      else if(!mUseTodayLayout) {
  
          ListView listView = (ListView) getActivity().findViewById(R.id.listview_forecast);
  
          listView.performItemClick(listView,
                  ListView.SCROLLBAR_POSITION_DEFAULT,
                  listView.getItemIdAtPosition(ListView.SCROLLBAR_POSITION_DEFAULT));
  
      }
  }
  </code></pre>
  
  <p>The stack trace is pasted below:</p>
  
  <pre><code>java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished
              at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1369)
              at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1383)
              at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
              at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
              at com.example.android.sunshine.app.MainActivity.onListItemClicked(MainActivity.java:200)
              at com.example.android.sunshine.app.ForecastFragment.onLoadFinished(ForecastFragment.java:285)
              at com.example.android.sunshine.app.ForecastFragment.onLoadFinished(ForecastFragment.java:46)
              at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
              at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
              at android.support.v4.content.Loader.deliverResult(Loader.java:104)
              at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
              at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
              at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
              at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
              at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
              at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
              at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5257)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>