I have pretty simple Fragment with ListView, CursorLoader and CursorAdapter. Everything is in single activity (just switching fragments). My problem is that onLoadFinished() is called in some situations twice based on where I put initLoader() call. Those situations are:
When initLoader() is put in onCreateView(), onActivityCreated() (recommended in documentation) onLoadFinished() is called twice after configuration change. There is explanation why by Rudik Krasniynos. But onLoadFinished() is called only once when popping newer fragment from backstack.
When initLoader() is placed in onResume()/onStart() method situation from above is reversed. Two calls onLoadFinished() for popping backstack and one for configuration change.
So the question is where or how to init Loader without calling onLoadFinished() twice or what to check to not init Loader twice. Thanks!
Code for replacing fragment:
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.root_view, fragment, tag);
ft.addToBackStack(null);
ft.commit();
Code for CursorAdapter:
//onCreateView
MyCursorAdapter mAdapter = new MyCursorAdapter(getActivity(), null, 0);
mList.setAdapter(mAdapter);
//in other/same lifecycle callbacks
getLoaderManager().initLoader(ID, null, this);
I'm using support library v18.
To avoid onLoadFinished
to be called more than once init your loader in onResume
.
I call it in onCreate and as far as I know this is the best practice... I'm using it right now and all work perfectly.
@Override
protected void onCreate(Bundle savedInstanceState)
.......
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
.......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With