-
-
Save udacityandroid/ee8fe10c04ec2d912c509ee3fb287149 to your computer and use it in GitHub Desktop.
Switch from AsyncTask to AsyncTaskLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
What about LoaderManager.LoaderCallbacks instead of LoaderCallbacks ?
Same
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
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>