Thursday, 12 September 2013

Calling getLoaderManager().restartLoader(...) from onLoadFinished(...)

Calling getLoaderManager().restartLoader(...) from onLoadFinished(...)

This is my problem: I want to init/restart a second loader from
onLoadFinished(...) method after the first loader has finished his job
(load finished), since i need some values from first loaders cursor. So
the second loader depends on some values, which the first loader delivers.
It works... but if a configuration change appears (rotation), it crashes
because while in this call, since it can happen after an activity's state
is saved. So there is no activity attached. If you call getActivity() for
example it returns null. Same thing happens if you call
getLoaderManager().XXX of course.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(FIRST_LOADER, null, this);
(...)
}
...
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case FIRST_LOADER:
data.moveToFirst();
value1 =
data.getString(data.getColumnIndex(DBHelper.COLUMN_FIRST));
value2 =
data.getString(data.getColumnIndex(DBHelper.COLUMN_SECOND));
getLoaderManager().restartLoader(SECOND_LOADER, null, this);
break;
case SECOND_LOADER:
(...)
}
}
But if I destroy loaders manually in onDestroy(...) method there are no
problems. Ok, it works that way, but it dont make me happy. So there must
be some other way to solve this.
(And Im sorry for my poor english :) i hope it is understandable :P)

No comments:

Post a Comment