I have a Fragment called AllSongsFragment
in a ViewPager
. The parent Activity
extends android.support.v4.app.FragmentActivity
.
I'm trying to implement LoaderManager
and CursorLoader
to get a cursor asynchronously. Below is the AllSongsFragment
:
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
public class AllSongsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public void onActivityCreated(Bundle savedInstanceState) {
.
.
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Toast.makeText(getActivity(), "YAYYYY", Toast.LENGTH_SHORT);
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = new String[] {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST};
CursorLoader cursorLoader = new CursorLoader(getActivity(), uri, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Toast.makeText(getActivity(), "finished", Toast.LENGTH_SHORT);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO
}
}
My problem is that onCreateLoader() or any of the LoaderManager callback methods aren't getting called. The app doesn't crash either and there is no Exceptions. What am I doing wrong?
You are using the support library classes LoaderManager
and CursorLoader
. Hence you need to use the call to the support LoaderManager
.
Replace
getLoaderManager().initLoader(0, null, this);
with
getActivity().getSupportLoaderManager().initLoader(0, null, (LoaderCallbacks<Cursor>)this);