Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created July 7, 2016 21:03
Show Gist options
  • Save udacityandroid/ee8fe10c04ec2d912c509ee3fb287149 to your computer and use it in GitHub Desktop.
Save udacityandroid/ee8fe10c04ec2d912c509ee3fb287149 to your computer and use it in GitHub Desktop.
Switch from AsyncTask to AsyncTaskLoader
public class EarthquakeActivity extends AppCompatActivity
implements LoaderCallbacks<List<Earthquake>> {
...
@Override
public Loader<List<Earthquake>> onCreateLoader(int i, Bundle bundle) {
// TODO: Create a new loader for the given URL
}
@Override
public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
// TODO: Update the UI with the result
}
@Override
public void onLoaderReset(Loader<List<Earthquake>> loader) {
// TODO: Loader reset, so we can clear out our existing data.
}
}
public class EarthquakeLoader extends AsyncTaskLoader<List<Earthquake>> {
public EarthquakeLoader(Context context, String url) {
super(context);
// TODO: Finish implementing this constructor
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<Earthquake> loadInBackground() {
// TODO: Implement this method
}
}
@KyawYe-Htut
Copy link

Please make an update for deprecated android class like this Loader lesson.
So do for many other out dated lessons.Thank a lot .

@KyawYe-Htut
Copy link

KyawYe-Htut commented Jul 5, 2020

loaderManager.initLoader(EARTHQUAKE_LOADER_ID,null, this).forceLoad();

//this is not valid right now . The ide request callback instead of this .What do I do?

@josikie
Copy link

josikie commented Jul 10, 2020

@KyawYe-Htut
use this code in the onCreate method in EarthquakeActivity :
LoaderManager loaderManager = getSupportLoaderManager();
loaderManager.initLoader(1, null, this);

don't use forceLoad() in EarthquakeActivity, use forceLoad() in EarthquakeLoader, ps : sorry my english bad:)

@tahilbansal
Copy link

The below things worked for me.

Step -1: Make the changes as in udacity/ud843-QuakeReport@6ac53a7?diff=split

Step -2 : use this code in the onCreate method instead of EarthquakeActivity class :
LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(1, null, this);

Step -3: Declare EarthqaukeLoader class as static as given below
public static class EarthquakeLoader extends AsyncTaskLoader<List>

@abdullahMorsi
Copy link

What about LoaderManager.LoaderCallbacks instead of LoaderCallbacks ?

Same

@Rohit570k
Copy link

Rohit570k commented Sep 28, 2021

Is there any reason why we made AsynTask an anonymous class of the Earthquake activity but AsyncTaskLoader is its own class?

so that we don't need to instantiate it for working and making inner class it can access parent class member and function directly,
in loader, we have callback method for establishing a connection that's why prefer to make a new class there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment