They are available to every Activity and Fragment.(就地取材)
They provide asynchronous loading of data.(用途)
They monitor the source of their data and deliver new results when the content changes.(感知数据变化)
They automatically reconnect to the last loader’s cursor when being recreated after a configuration change. Thus, they don’t need to re-query their data.
public class InTheatersTaskLoader extends AsyncTaskLoader<TaskLoaderResult<InTheaters>> {
private static final String TAG = InTheatersTaskLoader.class.getSimpleName(); private final Context mContext; private TaskLoaderResult<InTheaters> mResult;
public InTheatersTaskLoader(Context context) { super(context);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show();
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item); }
@Override public Loader<TaskLoaderResult<InTheaters>> onCreateLoader(int id, Bundle args) { Log.d(TAG, "onCreateLoader, id " + id);
return new InTheatersTaskLoader(this); }
@Override public void onLoadFinished(Loader<TaskLoaderResult<InTheaters>> loader, TaskLoaderResult<InTheaters> data) { if (data.getException() != null) { Log.e(TAG, data.getException().getMessage()); return; }