simplest demo app for loaders
1- Make a Loader ,this is done by creating a class that extends AsyncTaskLoader and implement its methods onStartLoading() and loadInBackground()
public class SimpleLoader extends AsyncTaskLoader<String> {
private static final String TAG = "flow";
public SimpleLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
super.onStartLoading();
forceLoad();
Log.i(TAG, "onStartLoading: without ForceLoad it won't go to loadinBackground");
Log.i(TAG, "onStartLoading: this will be called second "+ Thread.currentThread().getId());
}
@Override
public String loadInBackground() {
Log.i(TAG, "loadInBackground: this will be called third "+ Thread.currentThread().getId());
}
} //class closing bracket
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<D>
getSupportLoaderManager().initLoader(0, null, this);
-once we intiate loader with unique id 0 using getSupportLoaderManager().initLoader(0, null, this);
the system will check if loader with id 0 exist ? if no than go to call back method onCreateLoader() of LoaderManager
if yes than go to call back method onLoadFinished() of LoaderManager.
-onCreateLoader() has return type <D>
-inside this method we create the Loader.
new SimpleLoader(this);
-once Loader is created inside the onCreateLoader() ,the onStartLoading method of Loader is called .
-but after onStartLoading() method loadinBackground() is called only if we have put code forceLoad inside the onStartLoading method.
-loadinBackground() works on background thread.
-loadinBackground() has returns type <D> so it returns that value.
-this returned value goes directly to onCreateLoader() callback.
-at which point the Loader is Finished loading and onLoadFinished() callback is called.
-inside onLoadFinished() we can update the UI.