Characteristics and functions:
- Similar to AsyncTask asynchronous tasks, in the use of encapsulated Loader, it provides an off-the-shelf method to perform time-consuming operations in sub-threads, as well as a method to update UI pages automatically called after the end of sub-threads.
- Although Loader can also read network data, in general, Loader is used to read large amounts of database data.
- The loading task specified in Loader will automatically reload data when the page goes through onPause-onStop-onRestart-onStart-onResume (AsyncTask does not work).
1. Read the contents of the local database through Loader (self-produced and self-sold database created by SQLiteOpenHelper)
Use steps:
1. Initialize the LoaderManager object, through which the load task will be started later
//Initialize LoaderManager objects
manager = getLoaderManager();
2. Invoke initLoader method to start loading task
/*
* Start the load task
* 1. Assign a unique id identifier to the start-up asynchronous task
* 2. Bundle Object to carry data and pass values from the main thread to the Loader load task
* The function is similar: in asynchronous tasks, the parameters of the execute method can be used to pass in the address to be connected to the sub-thread.
* 3. LoaderCallbacks Interface objects that provide callback methods for specific moments during Loader's operation
*/
manager.initLoader(1, null, this);
3. Let the current class implement the LoaderCallbacks interface and implement the abstract method in the interface.
//Called when a Loader object is created
//The id and args in the parameter are the first and second parameters passed when the initLoader method is called.
//The return value is a Loader-type load task object that encapsulates the specific time-consuming load task to be performed.
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub
Log.i(TAG, "--------onCreateLoader "+Thread.currentThread().getName());
return new MyLoader(this);
}
//When the loading task is completed and the method is called, parameter 2 represents the loading result.
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// TODO Auto-generated method stub
Log.i(TAG, "--------onLoadFinished");
adapter.swapCursor(data);
// When Loader is used in conjunction with SimpleCursor Adapter, refreshing the data in the adapter requires only the data source on that side.
//The steps of notify DataSetChanged can be omitted
// adapter.notifyDataSetChanged();
}
//Called when the load task is reset
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
Log.i(TAG, "--------onLoaderReset");
}
4. Create a subclass of AsyncTaskLoader and return the subclass object as the return value of the onCreateLoader method
5. Manually add the onStartLoading method to the subclass of AsyncTaskLoader and manually call the forceLoad method in this method
6. Perform time-consuming operations in the loadInBackground method of the AsyncTaskLoader subclass and return the results as return values.
static class MyLoader extends AsyncTaskLoader<Cursor> {
private Context context;
public MyLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
Log.i(TAG, "----- MyLoader constractor");
this.context = context;
}
//Note: Don't forget to add the onStartLoadeing method manually and call the forceLoad method in this method
@Override
protected void onStartLoading() {
// TODO Auto-generated method stub
super.onStartLoading();
Log.i(TAG, "----- onStartLoading");
//If this method is not called, the load InBackground method below will not execute.
forceLoad();
}
@Override
public Cursor loadInBackground() {
// TODO Auto-generated method stub
Log.i(TAG, "----- loadInBackground "+Thread.currentThread().getName());
return new MyHelper(context).getCursor();
}
//Distribution results, called after the execution of time-consuming operations, and called after the onLoadFinished method runs
//Parameters represent the current loading result
@Override
public void deliverResult(Cursor data) {
// TODO Auto-generated method stub
super.deliverResult(data);
Log.i(TAG, "----- deliverResult");
}
}
7. Update UI operation according to loading result in onLoadFinished, the implementation method of LoaderCallbacks interface
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// TODO Auto-generated method stub
Log.i(TAG, "--------onLoadFinished");
adapter.swapCursor(data);
// When Loader is used in conjunction with SimpleCursor Adapter, refreshing the data in the adapter requires only the data source on that side.
//The steps of notify DataSetChanged can be omitted
// adapter.notifyDataSetChanged();
}
Start the running process of the program after loading the task by initLoader method:
1. Run the onCreateLoader method and determine whether the return value of the method is null. If it is null, the execution ends, not null, and continue downward. 2. Construction method of return value object (Loader subclass object) by running onCreateLoader method 3. The onStartLoadeing method that runs onCreateLoader method to return value objects (Loader subclass objects) And determine whether the forceLoad method is called in this method, if not, it stops. If there is a call, continue down 4. Run the loadInBackground method, in which the code runs in a sub-thread and can be processed in a time-consuming manner. 5. Running onLoadFinished Method 6. See if the delivery result method is overwritten in the onCreateLoader method return value object (Loader subclass object), and if so, run the method.
When a load task needs to be restarted:
//Start the load task and re-query the data
// The parameters of the initLoader method work the same way as those of the initLoader method.
Bundle args = new Bundle();
args.putString("str", newText);
manager.restartLoader(1, args, TwoActivity.this);
return false;
2. Realizing networking operation through Loader
3. Read open data through ContentProvider through Loader