Loader: onLoadFinished is called only once

I have one bootloader used in activity. I can start the bootloader and it is called onLoadFinished. When I update the data and call onContentChanged in the loader, I see that loadInBackground and deliverResult . Here the trail stops. I do not get any callback to onLoadFinished .

If I recreate an action (otherwise a change of orientation or restart), then it will behave the same.

I am using the bootloader and bootloader manager support-v4.

My SharedPreferenceLoader based on CommonsWare loader :

 public class SharedPreferencesLoader extends AsyncTaskLoader<SharedPreferences> implements SharedPreferences.OnSharedPreferenceChangeListener { private SharedPreferences prefs = null; private static final String TAG = SharedPreferencesLoader.class.getSimpleName(); public SharedPreferencesLoader(Context context) { super(context); } @Override public SharedPreferences loadInBackground() { Log.v(TAG, "wol: load in background"); prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); prefs.registerOnSharedPreferenceChangeListener(this); return (prefs); } @Override protected void onStartLoading() { if (prefs != null) { deliverResult(prefs); } if (takeContentChanged() || prefs == null) { forceLoad(); } } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.v(TAG, "wol: on shared preference changed."); onContentChanged(); } } 

Here's how the bootloader is used:

 public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<SharedPreferences> { private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportLoaderManager().initLoader(0, null, this); // fetch list of locations through an intent service. // This will save data to shared preferences and trigger onContentChanged MyIntentService.startActionGetLocations(this); } @Override public Loader<SharedPreferences> onCreateLoader(int i, Bundle bundle) { return new SharedPreferencesLoader(this); } @Override public void onLoadFinished(final Loader<SharedPreferences> sharedPreferencesLoader, final SharedPreferences sharedPreferences) { final List<PwStructure> locations = SharedPreferencesUtils.getLocations(sharedPreferences, this); Log.v(TAG, "wol: on load finished: "+locations); /* Do something with data. */ } @Override public void onLoaderReset(final Loader<SharedPreferences> sharedPreferencesLoader) { } } 

Update I found this in the LoaderManager source. It looks like the onLoadFinished call onLoadFinished not called if the data has the same reference.

+7
android commonsware-cwac android-support-library android-loader
source share
1 answer

Yes, it seems that with the current implementation of the LoaderManager, SharedPreferencesLoader is pretty tightly screwed. I blame it.

Thanks for pointing this out!

+4
source share

All Articles