Safwan Ahmad Safwan Ahmad - 4 months ago 45
Android Question

ListView's performItemClick() causes IllegalStateException when called inside onLoadFinished()

I am implementing this Two Pane UI. 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.

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

performItemClick()
on my list like this:

listView.performItemClick(listView,
ListView.SCROLLBAR_POSITION_DEFAULT,
listView.getItemIdAtPosition(ListView.SCROLLBAR_POSITION_DEFAULT));


I'm doing this in the
onLoadFinished()
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
java.lang.IllegalStateException
error.

I'd like to know why performing a click action in
onLoadFinished()
causes the exception whereas other calls on the list view like
smoothScrollToPosition()
get completed.

From other SO posts I can see that it can be done using a
Handler()
. 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
Handler()
. I am fairly new to Android and there are gaps in my understanding of its architecture.

Here's the full code that fails:

public void onLoadFinished(Loader<Cursor> 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));

}
}


The stack trace is pasted below:

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)

Answer

The IllegalStateException occurs not because of calling listView.performItemClick() but because this call was ultimately causing a fragment transaction. Fragment transactions are disallowed in asynchronous callbacks like onLoadFinished(). I found some useful information here.