id, Bundle args) { CursorLoader loader = new CursorLoader(getApplicationContext()); loader.setUri(SOME_URI); return loader; } // TODO }); create a Loader that performs your operation
id, Bundle args) { CursorLoader loader = new CursorLoader(getApplicationContext()); loader.setUri(SOME_URI); return loader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { // cursor loaded } // TODO }); implement an action to be taken when data is loaded
id, Bundle args) { CursorLoader loader = new CursorLoader(getApplicationContext()); loader.setUri(SOME_URI); return loader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { // cursor loaded } @Override public void onLoaderReset(Loader<Cursor> loader) { // does anybody know what to do here? } }); get rid of any references to the data previously loaded
{ getLoaderManager().initLoader(MAGIC_ID, null, callback); } findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loading = true; getLoaderManager().initLoader(MAGIC_ID, null, callback); } }); } 1 save your state; don’t forget about onSaveInstanceState 2 attach a callback if bundle has your flag set
{ getLoaderManager().initLoader(MAGIC_ID, null, callback); } findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loading = true; getLoaderManager().restartLoader(MAGIC_ID, null, callback); } }); } e.g. if you want to submit an order ensures your operation will be performed
} protected void onStartLoading() { } protected boolean onCancelLoad() { } protected void onForceLoad() { } protected void onStopLoading() { } protected void onAbandon() { } protected void onReset() { } } Loader abstraction Serves for solving one main problem: control your async operation execution. Control is done by LoaderManager through binding to UI lifecycle. Delivered data is retained.