Difference between restartLoader and onContentChanged

I currently have a bootloader

@Override public Loader<List<HomeMenuRowInfo>> onCreateLoader(int arg0, Bundle bundle) { return new HomeMenuRowInfosLoader(this.getSherlockActivity()); } 

Sometimes I need to ask the bootloader to reboot again due to content changes. I will do it.

 this.getLoaderManager().getLoader(0).onContentChanged(); 

However, I want to pass additional package information to the onCreateLoader when the content changes. I understand using onContentChanged , there is no way to do this.

The only way to find out is

 this.getLoaderManager().restartLoader(0, bundle, this); 

I was wondering if there are any significant differences in Loader behavior using restartLoader instead of onContentChanged , in addition to being able to pass to the package?

+6
source share
3 answers

I think the main difference is that the restartLoader method destroys the old bootloader with the same identifier and launches the new one, while the onContentChanged method either forces the bootloader to load ( forceLoad ) or simply sets a flag that indicates that the contents changed while the bootloader stopped . In the second case, the "owner" of the bootloader remains responsible for its (re) loading after changing the content. I assume this is done automatically using the loaderManager , as in the case of restartLoader .

If you decide to use the restartLoader method, you should keep in mind the destruction of the old bootloader and possible consequences for your application, such as slow reinitialization, etc.

You can see the documentation of the methods for more information - restartLoader and onContentChanged

Also note that the old bootloader is destroyed when the new one finishes its work.

+4
source

A state diagram should help you understand how to use the API. The first time, I also spent many minutes to make the photo understandable. Use my work!

Article: https://plus.google.com/117981280628062796190/posts/8b9RmQvxudb

Loader states diagram

+1
source

call restartLoader every time you need fresh data, or you want to change your arguments for the cursor.

If you use initLoader to load data

 lm = fragment.getLoaderManager(); lm.initLoader(LOADER_ID_LIST, null, this); 

Each initLoader call returns the same cursor to onLoadFinished . The onCreateLoader method will only be called the first time initLoader is called. Therefore, you cannot change the request. You get the same data for the onLoadFinished method.

 @Override public void onLoadFinished( android.support.v4.content.Loader<Cursor> loader, Cursor cursor) { switch (loader.getId()) { case LOADER_ID_LIST: // The asynchronous load is complete and the data // load a list of populateFromCursor(cursor); break; case LOADER_ID_ENTRY: populateFromCursor(cursor); break; } // The listview now displays the queried data. } 

So, to get the new cursor data on onLoadFinished , use restartLoader and you can pass the package information if you want. Below I pass null because there is a global variable.

 lm = fragment.getLoaderManager(); lm.restartLoader(LOADER_ID_LIST, null, this); 

The loaderManager manager then calls onCreateLoaderMethod .

 @Override public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) { android.support.v4.content.Loader<Cursor> ret = null; // Create a new CursorLoader with the following query parameters. switch (id) { // load the entire list case LOADER_ID_LIST: String sortOrder = null; switch (mSortOrder) { case 0: sortOrder = RidesDatabaseHandler.KEY_DATE_UPDATE + " desc"; break; case 1: sortOrder = RidesDatabaseHandler.KEY_ID + " desc"; break; case 2: sortOrder = RidesDatabaseHandler.KEY_NAME; } return new CursorLoader(context, RidesDatabaseProvider.CONTENT_URI, PROJECTION, null, null, sortOrder); // load a single item case LOADER_ID_ENTRY: sortOrder = null; String where = RidesDatabaseHandler.KEY_ID + "=?"; String[] whereArgs = new String[] { Integer.toString(lastshownitem) }; return new CursorLoader(context, RidesDatabaseProvider.CONTENT_URI, PROJECTION, where, whereArgs, null); } return ret; } 

Notes. You do not need to call initLoader , you can call restartLoader every time if you really want the same data to be returned from a previous request. You don't need to call onContentChanged() , but the docs say Loader.ForceLoadContentObserver is done for you.

0
source

Source: https://habr.com/ru/post/945750/


All Articles