Jack Wang
Jack Wang

Reputation: 144

About auto update data with use LoaderCallbacks

I want to realize, when entering Activity or Fragment, is the first time to load data from the network, if not connected to the Internet or to fail to load, to load the local data, at present I used is the first through the getLoaderManager () .InitLoader (0, null, this); in the onLoadFinished method. Then, go to the network to update the data, but I have a problem, because onLoadFinished will be called many times, so many times to the network will request to update the data, I do not know is not my flow problem, or I do wrong, someone who can give me some help or advice? Thank you..

    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
         mAdapter.swapCursor(cursor);
         fillDataFromNet();
    }

Upvotes: 0

Views: 76

Answers (1)

Yuichi Araki
Yuichi Araki

Reputation: 3458

Instead of calling fillDataFromNet inside of onLoadFinished, you can call it along with initLoader. It is likely that the loader will finish before the network finishes. The data will then passed to the loader and onLoadFinished will be fired again.

fillDataFromNet();
getLoaderManager().initLoader(SOME_LOADER_ID, someArguments, this);

Upvotes: 1

Related Questions