The callbacks are only invoked after the loader is initiated (initLoader
)or restarted (restartLoader
). With initLoader
the cached result can be used after an orientation change but it has to be called explicitly to deliver the data in onLoadFinished
. Hence, you have to call it in your onCreate
method with the itemPosition
as the loader id, but only if an itemPosition
is already set.
I've stripped your example code to illustrate what I mean:
// static so that it survives orientation change.
private static int mSelectedItemPosition = -1; // -1 = Not selected
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mSelectedItemPosition != -1) {
getLoaderManager().initLoader(mSelectedItemPosition, null, MainActivity.this);
}
getActionBar().setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if(itemPosition == 0 || itemPosition ==1) {
mSelectedItemPosition = 0;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
else if(itemPosition == 2) {
mSelectedItemPosition = itemPosition;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
else {
mSelectedItemPosition = itemPosition;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
...
}